:root {
  color-scheme: light dark;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
  background-color: #f7f7f7;
  color: #222;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

body.auth-locked {
  overflow: hidden;
}

#protected-app[hidden] {
  display: none !important;
}

body.auth-locked #protected-app {
  pointer-events: none;
  user-select: none;
  filter: blur(3px);
  opacity: 0;
  transition: opacity 160ms ease;
}

body:not(.auth-locked) #protected-app {
  pointer-events: auto;
  user-select: auto;
  filter: none;
  opacity: 1;
}

header {
  padding: 1.5rem 2rem 1rem 2rem;
  background-color: #1f2933;
  color: #f5f5f5;
}

header h1 {
  margin: 0 0 0.75rem 0;
  font-size: 1.75rem;
}

form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

label {
  font-weight: 600;
}

.input-row {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

input[type="text"] {
  flex: 1 1 320px;
  padding: 0.65rem 0.75rem;
  border-radius: 6px;
  border: 1px solid #c4c4c4;
  font-size: 1rem;
}

button {
  cursor: pointer;
  padding: 0.65rem 1.2rem;
  border-radius: 6px;
  border: none;
  background-color: #2563eb;
  color: #fff;
  font-weight: 600;
  font-size: 1rem;
  transition: background-color 0.2s ease;
}

button:hover,
button:focus {
  background-color: #1d4ed8;
}

#auth-overlay {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.88);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  z-index: 1000;
}

body.auth-locked #auth-overlay {
  display: flex;
}

body:not(.auth-locked) #auth-overlay {
  display: none;
}

.auth-form {
  background: #ffffff;
  border-radius: 12px;
  padding: 2.5rem;
  box-shadow: 0 22px 40px rgba(17, 24, 39, 0.25);
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.auth-form h2 {
  margin: 0;
  font-size: 1.35rem;
  text-align: center;
  color: #1f2933;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.auth-field label {
  font-size: 0.88rem;
  color: #364152;
}

.auth-field input {
  padding: 0.7rem 0.85rem;
  border: 1px solid #cbd5f5;
  border-radius: 8px;
  font-size: 0.95rem;
  box-sizing: border-box;
  width: 100%;
  height: 2.9rem;
  flex: none;
}

.auth-field input:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}

.auth-error {
  min-height: 1.1rem;
  margin: 0;
  font-size: 0.85rem;
  color: #dc2626;
  text-align: center;
}

.auth-submit {
  padding: 0.75rem;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, #1d4ed8, #2563eb);
  color: #ffffff;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.auth-submit:hover,
.auth-submit:focus {
  transform: translateY(-1px);
  box-shadow: 0 12px 24px rgba(37, 99, 235, 0.25);
}

.hint {
  margin: 0.5rem 0 0 0;
  font-size: 0.9rem;
  color: #d1d9ec;
}

main {
  flex: 1 1 auto;
  display: flex;
  gap: 2rem;
  padding: 1.5rem 2rem 2rem 2rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#navigator {
  width: 260px;
  min-width: 180px;
  max-width: 320px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  padding: 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  transition: width 0.2s ease, min-width 0.2s ease;
}

.navigator-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.navigator-header h2 {
  margin: 0;
  font-size: 1.15rem;
  color: #1f2933;
}

#toggle-navigator {
  background-color: transparent;
  color: #2563eb;
  border: none;
  font-weight: 600;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
}

#toggle-navigator:hover,
#toggle-navigator:focus {
  text-decoration: underline;
}

#navigator-body {
  overflow: auto;
  flex: 1 1 auto;
}

.add-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

#add-favorite-input {
  flex: 1 1 auto;
  border-radius: 6px;
  border: 1px solid #cbd5e1;
  padding: 0.55rem 0.6rem;
  font-size: 0.95rem;
}

#add-favorite-form button {
  padding: 0.55rem 0.9rem;
  font-size: 0.95rem;
}

.navigator-status {
  margin: 0;
  color: #6b7280;
  font-size: 0.9rem;
}

.navigator-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.navigator-item + .navigator-item {
  margin-top: 0.35rem;
}

.navigator-row {
  display: flex;
  align-items: flex-start;
  gap: 0.4rem;
}

.navigator-row button,
.navigator-row .dir-name {
  background: none;
  border: none;
  font: inherit;
  cursor: pointer;
  padding: 0;
  color: #0f172a;
  text-align: left;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  flex: 1 1 auto;
  white-space: normal;
  word-break: break-word;
}

.favorite-delete {
  margin-left: 0.5rem;
  background: none;
  border: 1px solid transparent;
  color: #dc2626;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.35rem;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  min-height: 28px;
  transition: all 0.2s ease;
  opacity: 0.7;
}

.favorite-delete:hover,
.favorite-delete:focus {
  background-color: rgba(220, 38, 38, 0.12);
  border-color: rgba(220, 38, 38, 0.3);
  opacity: 1;
  transform: scale(1.05);
  box-shadow: 0 2px 4px rgba(220, 38, 38, 0.15);
}

.favorite-delete:active {
  transform: scale(0.95);
  background-color: rgba(220, 38, 38, 0.18);
}

.navigator-row .dir-name .dir-content {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
}

.navigator-row .dir-name .dir-label {
  font-size: 0.95rem;
}

.navigator-row .dir-name .dir-timestamp {
  font-size: 0.75rem;
  color: #1d4ed8;
  background-color: #eff6ff;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.navigator-row .dir-name:hover,
.navigator-row .dir-name:focus {
  text-decoration: underline;
  color: #1d4ed8;
}

.navigator-row .dir-name.active {
  color: #2563eb;
  font-weight: 600;
}

.collapsed-nav {
  width: 36px !important;
  min-width: 36px !important;
  padding: 1rem 0.5rem !important;
}

.collapsed-nav #navigator-body {
  display: none;
}

.collapsed-nav .navigator-header h2 {
  display: none;
}

.collapsed-nav #toggle-navigator {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  padding: 0;
}

#article-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  overflow: auto;
  flex: 1 1 0;
  min-width: 0;
}

#status-message {
  margin-bottom: 1rem;
  font-weight: 600;
  color: #555;
}

#status-message.success {
  color: #2f8f2f;
}

#status-message.warning {
  color: #c17c00;
}

.table-toggle {
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.95rem;
}

.article-actions {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.article-actions .secondary {
  align-self: flex-start;
}

.table-block[hidden] {
  display: none;
}

.secondary {
  background-color: #e2e8f0;
  color: #1f2933;
}

.secondary:hover,
.secondary:focus {
  background-color: #cbd5e1;
}

#article-content h1,
#article-content h2,
#article-content h3 {
  color: #1f2933;
}

#article-content code,
#citation-body code {
  background: #f0f4ff;
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  font-size: 0.9rem;
}

#article-content pre,
#citation-body pre {
  background: #f1f5f9;
  color: #0f172a;
  padding: 1rem;
  border-radius: 6px;
  overflow: auto;
  border: 1px solid #cbd5e1;
}

.citation {
  color: #2563eb;
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
  display: inline;
  border: none;
  background: none;
  padding: 0;
  font: inherit;
}

.citation:focus,
.citation:hover {
  text-decoration: underline;
}

.active-citation {
  background-color: rgba(37, 99, 235, 0.1);
  border-radius: 4px;
  padding: 0 0.2rem;
}

#citation-panel {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  padding: 1rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 320px;
  min-width: 40px;
  max-width: none;
  flex: 0 0 auto;
  position: sticky;
  top: 2.5rem;
  max-height: calc(100vh - 3.5rem);
}

#citation-panel[hidden] {
  display: none;
}

#tree-view {
  flex: 1 1 0;
  min-width: 0;
  background-color: #fff;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#tree-view[hidden] {
  display: none;
}

.tree-header {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.tree-header-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.tree-header h2 {
  margin: 0;
  font-size: 1.4rem;
  color: #1f2933;
}

.tree-status {
  margin: 0;
  font-size: 0.95rem;
  color: #4b5563;
  min-height: 1.2rem;
}

.tree-status.error {
  color: #b91c1c;
}

.tree-status .highlight-note {
  font-weight: 600;
  color: #b91c1c;
  margin-left: 0.5rem;
}

.tree-body {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(300px, 1fr) auto auto;
  gap: 0;
  overflow: hidden;
}

#tree-network {
  min-width: 0;
  min-height: 400px;
  max-height: calc(100vh - 220px);
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background-color: #f8fafc;
}

#tree-drag-handle {
  width: 8px;
  background-color: #e2e8f0;
  cursor: col-resize;
  position: relative;
  transition: background-color 0.15s ease;
  user-select: none;
}

#tree-drag-handle:hover,
#tree-drag-handle:focus {
  background-color: #cbd5e1;
  outline: none;
}

#tree-drag-handle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 3px;
  height: 40px;
  background-color: #94a3b8;
  border-radius: 2px;
  pointer-events: none;
}

#tree-details {
  width: 360px;
  min-width: 240px;
  background-color: #f8fafc;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  padding: 1rem;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#tree-details h3 {
  margin: 0;
  font-size: 1.1rem;
  color: #1f2933;
}

.tree-details-content {
  flex: 1 1 auto;
  overflow: auto;
  font-size: 0.95rem;
  color: #334155;
}

.tree-details-content .placeholder {
  margin: 0;
  color: #6b7280;
}

#citation-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#citation-header h2 {
  margin: 0;
  font-size: 1.2rem;
  color: #1f2933;
}

#close-citation {
  background-color: transparent;
  color: #475569;
  font-size: 1.5rem;
  padding: 0 0.4rem;
}

#close-citation:hover,
#close-citation:focus {
  background-color: rgba(15, 23, 42, 0.08);
}

#citation-body {
  flex: 1;
  overflow-y: auto;
  font-size: 0.95rem;
  display: flex;
  flex-direction: column;
}

#citation-body > * {
  flex-shrink: 0;
}

#citation-body h3 {
  margin-top: 0;
}

.citation-section {
  margin-bottom: 1rem;
}

.citation-heading {
  margin: 0 0 0.35rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: #1f2933;
}

.citation-description p:last-child {
  margin-bottom: 0;
}

.citation-query {
  margin: 0;
  white-space: pre-wrap;
}

.citation-sql {
  margin: 0;
  font-size: 0.85rem;
  line-height: 1.45;
  white-space: pre-wrap;
}

.citation-links p {
  margin: 0;
}

.citation-url-section {
  margin-bottom: 1rem;
}

.citation-external-url {
  word-break: break-all;
  margin: 0;
  font-size: 0.92rem;
}

.citation-external-wrapper {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.citation-url-missing {
  color: #b91c1c;
  font-style: italic;
}

.citation-open-button {
  display: inline-block;
  padding: 0.65rem 1.2rem;
  background: linear-gradient(135deg, #1d4ed8, #2563eb);
  color: #ffffff;
  font-weight: 600;
  font-size: 0.95rem;
  text-decoration: none;
  border-radius: 8px;
  text-align: center;
  transition: transform 120ms ease, box-shadow 120ms ease;
  margin-bottom: 1rem;
  align-self: flex-start;
}

.citation-open-button:hover,
.citation-open-button:focus {
  transform: translateY(-1px);
  box-shadow: 0 8px 16px rgba(37, 99, 235, 0.25);
}

.citation-iframe-container {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  position: relative;
}

.citation-iframe {
  width: 100%;
  min-height: 0;
  flex: 1;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background-color: #fff;
}

.resizing .citation-iframe {
  pointer-events: none;
}

.citation-iframe-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background-color: #fef2f2;
  border: 2px dashed #fca5a5;
  border-radius: 8px;
  text-align: center;
  gap: 0.75rem;
  flex: 1;
  min-height: 200px;
}

.citation-iframe-error[hidden] {
  display: none;
}

.citation-iframe-error-icon {
  font-size: 3rem;
  margin: 0;
  opacity: 0.7;
}

.citation-iframe-error-message {
  margin: 0;
  color: #991b1b;
  font-weight: 600;
  font-size: 0.95rem;
}

.citation-iframe-error-link {
  display: inline-block;
  padding: 0.6rem 1.1rem;
  background-color: #2563eb;
  color: #ffffff;
  font-weight: 600;
  font-size: 0.9rem;
  text-decoration: none;
  border-radius: 6px;
  transition: background-color 0.2s ease, transform 120ms ease;
  margin-top: 0.5rem;
}

.citation-iframe-error-link:hover,
.citation-iframe-error-link:focus {
  background-color: #1d4ed8;
  transform: translateY(-1px);
}

.placeholder {
  color: #6b7280;
  font-style: italic;
}

.error {
  color: #b91c1c;
}

.success {
  color: #166534;
}

.warning {
  color: #b45309;
}

#drag-handle {
  width: 6px;
  background: linear-gradient(0deg, #cbd5e1 0%, #e2e8f0 100%);
  border-radius: 6px;
  cursor: col-resize;
  align-self: stretch;
  position: relative;
  flex: 0 0 auto;
}

#drag-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 32px;
  transform: translate(-50%, -50%);
  background-color: #94a3b8;
  border-radius: 999px;
}

.resizing {
  user-select: none;
  cursor: col-resize;
}

@media (max-width: 960px) {
  main {
    flex-direction: column;
  }

  #drag-handle {
    display: none;
  }

  #navigator {
    order: -2;
    width: 100%;
    min-width: 0;
    max-width: none;
    gap: 1rem;
  }

  #navigator-body {
    max-height: 240px;
  }

  .collapsed-nav {
    width: 100% !important;
    min-width: 0 !important;
    padding: 1rem 1rem !important;
  }

  .collapsed-nav #navigator-body {
    display: block;
  }

  .collapsed-nav .navigator-header h2 {
    display: inline;
  }

  .collapsed-nav #toggle-navigator {
    writing-mode: initial;
    transform: none;
  }

  #citation-panel {
    order: -1;
    width: 100%;
    max-width: none;
    position: static;
  }
}
