:root {
  --bg: #e9e9e5;
  --surface: #f7f7f3;
  --surface-strong: #ffffff;
  --ink: #17191a;
  --ink-soft: #4f5354;
  --line: #b7b9b6;
  --line-dark: #747877;
  --accent: #f4c542;
  --accent-dark: #8a6700;
  --danger: #a72d27;
  --danger-bg: #fff0ee;
  --success: #246b46;
  --radius: 3px;
  --shadow: 4px 4px 0 rgba(23, 25, 26, 0.13);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html { background: var(--bg); color-scheme: light; }

body {
  margin: 0;
  min-width: 280px;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  color: var(--ink);
  background:
    linear-gradient(rgba(23, 25, 26, 0.035) 1px, transparent 1px),
    var(--bg);
  background-size: 100% 24px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select { font: inherit; }
button, select, input[type="file"] { touch-action: manipulation; }
button { color: inherit; }

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid rgba(244, 197, 66, 0.6);
  outline-offset: 2px;
}

input, textarea, select {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line-dark);
  border-radius: 0;
  padding: 12px;
  color: var(--ink);
  background: var(--surface-strong);
  appearance: none;
  -webkit-appearance: none;
}

textarea {
  min-height: 180px;
  resize: vertical;
  line-height: 1.65;
}

select {
  padding-right: 36px;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--ink) 50%),
    linear-gradient(135deg, var(--ink) 50%, transparent 50%);
  background-position: calc(100% - 18px) 21px, calc(100% - 12px) 21px;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}

img { max-width: 100%; }

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

[hidden] { display: none !important; }

.startup-screen {
  position: fixed;
  z-index: 200;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(20px, env(safe-area-inset-top, 0px)) 20px max(20px, env(safe-area-inset-bottom, 0px));
  overflow: hidden;
  background: #121415;
  color: #f7f7f3;
  opacity: 1;
  visibility: visible;
  transition: opacity 180ms ease-out, visibility 180ms linear;
}

.startup-screen.is-exiting {
  opacity: 0;
  visibility: hidden;
}

.startup-core {
  width: min(78vw, 360px);
  text-align: center;
}

.startup-code,
.startup-status {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.15em;
}

.startup-code {
  display: block;
  margin-bottom: 12px;
  color: #8c9090;
  font-size: 0.58rem;
  opacity: 0;
  animation: startup-fade 220ms ease-out 80ms forwards;
}

.startup-line {
  position: relative;
  width: 100%;
  height: 2px;
  overflow: hidden;
  background: rgba(244, 197, 66, 0.2);
  transform: scaleX(0);
  transform-origin: left center;
  animation: startup-line-grow 560ms cubic-bezier(.2, .8, .2, 1) 100ms forwards;
}

.startup-line::after {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 28%;
  background: #f4c542;
  box-shadow: 0 0 10px rgba(244, 197, 66, 0.5);
  transform: translateX(-110%);
  animation: startup-scan 500ms ease-out 170ms forwards;
}

.startup-title {
  margin: 17px 0 7px;
  font-size: clamp(1.25rem, 6vw, 1.65rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  opacity: 0;
  transform: translateY(5px);
  animation: startup-title-in 300ms ease-out 330ms forwards;
}

.startup-status {
  margin: 0;
  color: #aeb1af;
  font-size: 0.62rem;
  opacity: 0;
  animation: startup-fade 220ms ease-out 510ms forwards;
}

@keyframes startup-line-grow {
  to { transform: scaleX(1); }
}

@keyframes startup-scan {
  to { transform: translateX(370%); }
}

@keyframes startup-title-in {
  to { opacity: 1; transform: translateY(0); }
}

@keyframes startup-fade {
  to { opacity: 1; }
}

.app-shell {
  width: min(100%, 780px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 0 14px calc(92px + var(--safe-bottom));
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: calc(18px + env(safe-area-inset-top, 0px)) 0 18px;
  border-bottom: 2px solid var(--ink);
}

.brand-block { display: flex; gap: 10px; min-width: 0; }
.brand-index, .section-code, .search-code, .eyebrow, .count-label, .status-chip {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.08em;
}
.brand-index {
  flex: none;
  padding: 5px 6px;
  background: var(--accent);
  color: var(--ink);
  font-size: 0.66rem;
  font-weight: 800;
}
.eyebrow { margin: 0 0 4px; color: var(--ink-soft); font-size: 0.62rem; }
h1 { margin: 0; font-size: clamp(1.35rem, 6vw, 1.8rem); line-height: 1.15; }
.status-chip {
  flex: none;
  max-width: 110px;
  padding: 5px 7px;
  border: 1px solid var(--line-dark);
  background: var(--surface);
  font-size: 0.62rem;
  line-height: 1.3;
  text-align: center;
}
.status-chip.is-ready { border-color: var(--success); color: var(--success); }
.status-chip.is-error { border-color: var(--danger); color: var(--danger); }

.error-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
  padding: 10px 12px;
  border-left: 4px solid var(--danger);
  background: var(--danger-bg);
  color: #68201c;
  line-height: 1.45;
}

.view { display: none; padding: 18px 0 8px; }
.view.is-active { display: block; }

.terminal-search {
  position: relative;
  padding: 12px;
  border: 1px solid var(--ink);
  border-top: 5px solid var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.home-search { margin-top: 24px; }
.terminal-search::before {
  content: "SEARCH CONTROL";
  position: absolute;
  top: -5px;
  right: 8px;
  padding: 0 5px;
  color: var(--accent);
  background: var(--ink);
  font: 700 0.56rem/1 ui-monospace, monospace;
  letter-spacing: 0.12em;
}
.search-field {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  border-bottom: 1px solid var(--line-dark);
  background: var(--surface-strong);
}
.search-field input { min-width: 0; border: 0; background: transparent; }
.search-code { padding-left: 11px; color: var(--accent-dark); font-size: 0.7rem; font-weight: 800; }
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 12px; }
.filter-grid label > span { display: block; margin-bottom: 5px; color: var(--ink-soft); font-size: 0.72rem; }
.filter-grid select { min-width: 0; font-size: 0.9rem; }
.filter-summary { margin-top: 10px; color: var(--ink-soft); font-size: 0.72rem; }

.dashboard-section { padding: 24px 0 4px; }
.section-heading, .view-heading, .module-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.section-heading { padding-bottom: 8px; border-bottom: 1px solid var(--ink); }
.section-heading h2, .view-heading h2, .module-heading h3 { margin: 2px 0 0; overflow-wrap: anywhere; }
.section-heading h2 { font-size: 1.05rem; }
#folder-entry-title { font-size: 1.2rem; }
.view-heading { align-items: center; justify-content: flex-start; }
.view-heading h2 { font-size: 1.4rem; }
#folders-title { font-size: 1.55rem; }
.module-heading { align-items: center; margin-bottom: 10px; }
.module-heading h3 { font-size: 1rem; }
.section-code { color: var(--ink-soft); font-size: 0.62rem; font-weight: 700; }
.count-label { color: var(--ink-soft); font-size: 0.7rem; }

.folder-shortcuts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.folder-shortcut {
  position: relative;
  min-height: 70px;
  padding: 12px 10px;
  overflow: hidden;
  border: 1px solid var(--line-dark);
  border-left: 4px solid var(--accent);
  background: var(--surface);
  text-align: left;
}
.folder-shortcut strong { display: block; margin-bottom: 5px; overflow-wrap: anywhere; }
.folder-shortcut small { color: var(--ink-soft); }

.chip-list { display: flex; flex-wrap: wrap; gap: 8px; min-width: 0; }
.tag-chip {
  min-height: 44px;
  max-width: 100%;
  padding: 8px 11px;
  overflow-wrap: anywhere;
  border: 1px solid var(--line-dark);
  background: var(--surface);
  font-size: 0.82rem;
}
.tag-chip::before { content: "# "; color: var(--accent-dark); font-weight: 800; }
.tag-chip.is-selected { border-color: var(--ink); background: var(--ink); color: white; }
.tag-chip.is-selected::before { color: var(--accent); }

.record-list { display: grid; gap: 10px; }
.record-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  width: 100%;
  min-width: 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--line-dark);
  background: var(--surface);
  text-align: left;
  box-shadow: 2px 2px 0 rgba(23, 25, 26, 0.08);
}
.record-card-main { min-width: 0; padding: 12px; }
.record-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-bottom: 7px; color: var(--ink-soft); font-size: 0.65rem; }
.record-card h3 { margin: 0 0 6px; font-size: 1rem; line-height: 1.4; overflow-wrap: anywhere; }
.record-excerpt { margin: 0; color: var(--ink-soft); font-size: 0.86rem; line-height: 1.55; overflow-wrap: anywhere; white-space: pre-wrap; }
.record-card .chip-list { margin-top: 9px; gap: 5px; }
.record-card .mini-chip { padding: 3px 6px; border: 1px solid var(--line); font-size: 0.65rem; overflow-wrap: anywhere; }
.record-thumb {
  align-self: stretch;
  width: 84px;
  min-height: 104px;
  object-fit: cover;
  background: #d4d5d1;
  border-left: 1px solid var(--line-dark);
}
.record-arrow { align-self: center; padding-right: 12px; color: var(--ink-soft); }

.empty-state {
  padding: 26px 16px;
  border: 1px dashed var(--line-dark);
  color: var(--ink-soft);
  background: rgba(247, 247, 243, 0.65);
  text-align: center;
  line-height: 1.6;
}
.empty-state strong { display: block; color: var(--ink); }

.text-button, .icon-button, .back-button, .nav-item {
  border: 0;
  background: transparent;
  cursor: pointer;
}
.text-button { min-height: 44px; padding: 8px 4px; color: #654f08; text-decoration: underline; text-underline-offset: 3px; }
.icon-button { min-width: 44px; min-height: 44px; font-size: 1.35rem; }
.icon-button.compact { min-width: 44px; min-height: 44px; }
.back-button {
  flex: none;
  width: 48px;
  height: 48px;
  border: 1px solid var(--line-dark);
  background: var(--surface);
  font-size: 1.35rem;
}
.danger-text { color: var(--danger); }

.editor-form { display: grid; gap: 18px; }
.field-block { display: block; }
.field-label { display: flex; justify-content: space-between; margin-bottom: 7px; font-weight: 700; }
.field-label small, .muted { color: var(--ink-soft); font-size: 0.72rem; font-weight: 400; }
.form-module {
  padding: 12px;
  border: 1px solid var(--line-dark);
  background: rgba(247, 247, 243, 0.72);
}
.file-picker {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 68px;
  padding: 10px 12px;
  border: 1px dashed var(--line-dark);
  background: var(--surface-strong);
  cursor: pointer;
}
.file-picker input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.file-picker-icon { font-size: 1.7rem; color: var(--accent-dark); }
.file-picker strong, .file-picker small { display: block; }
.file-picker small { margin-top: 3px; color: var(--ink-soft); }
.image-preview-wrap { margin-top: 12px; }
.image-preview-wrap img { display: block; width: 100%; max-height: 55vh; object-fit: contain; border: 1px solid var(--line-dark); background: #d5d6d2; }
.image-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.inline-create { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; margin-top: 12px; }
.prominent-create { margin: 18px 0; }
.timestamp-panel { padding: 10px 12px; border-left: 3px solid var(--line-dark); color: var(--ink-soft); background: rgba(255,255,255,.45); font-size: 0.75rem; line-height: 1.7; }
.form-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; padding-bottom: 20px; }
.form-actions .separated { grid-column: 1 / -1; margin-top: 18px; }

.primary-button, .secondary-button, .danger-button {
  min-height: 48px;
  padding: 10px 15px;
  border-radius: 0;
  font-weight: 700;
  cursor: pointer;
}
.primary-button { border: 1px solid var(--ink); background: var(--accent); color: var(--ink); }
.secondary-button { border: 1px solid var(--line-dark); background: var(--surface); color: var(--ink); }
.danger-button { border: 1px solid var(--danger); background: var(--danger-bg); color: var(--danger); }
button:disabled { cursor: not-allowed; opacity: 0.55; }

.view-intro { margin: -2px 0 0 58px; color: var(--ink-soft); font-size: 0.84rem; line-height: 1.55; }
.folder-tree { display: grid; gap: 8px; }
.folder-node { min-width: 0; }
.folder-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 48px;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 4px;
  row-gap: 2px;
  min-width: 0;
  min-height: 56px;
  margin-left: min(calc(var(--depth, 0) * 15px), 60px);
  padding: 7px;
  border: 1px solid var(--line-dark);
  border-left: 4px solid var(--accent);
  background: var(--surface);
}
.folder-row::before {
  grid-column: 1;
  grid-row: 1 / span 2;
  align-self: start;
  padding-top: 7px;
  content: attr(data-index);
  color: var(--ink-soft);
  font: 0.62rem ui-monospace, monospace;
}
.folder-main { grid-column: 2; grid-row: 1; min-width: 0; }
.folder-main strong { display: block; font-size: 1.05rem; line-height: 1.35; overflow-wrap: anywhere; }
.folder-main small { color: var(--ink-soft); }
.folder-actions { grid-column: 2 / 4; grid-row: 2; display: flex; flex-wrap: wrap; justify-content: flex-end; }
.folder-actions button { min-width: 44px; min-height: 44px; border: 0; background: transparent; font-size: 0.76rem; }
.folder-actions .delete-action { color: var(--danger); }
.fold-toggle {
  grid-column: 3;
  grid-row: 1;
  width: 48px;
  min-height: 48px;
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  font-size: 1.3rem;
  font-weight: 600;
}
.fold-spacer { grid-column: 3; grid-row: 1; display: block; width: 48px; }

.manager-list { display: grid; gap: 8px; }
.manager-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid var(--line-dark);
  background: var(--surface);
}
.manager-row strong { min-width: 0; overflow-wrap: anywhere; }
.manager-actions { display: flex; }
.manager-actions button { min-width: 48px; min-height: 44px; border: 0; background: transparent; font-size: 0.78rem; }
.manager-actions .delete-action { color: var(--danger); }

.fab {
  position: fixed;
  z-index: 20;
  right: max(18px, calc((100vw - 780px) / 2 + 18px));
  bottom: calc(74px + var(--safe-bottom));
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 50px;
  padding: 9px 15px;
  border: 1px solid var(--ink);
  border-right: 5px solid var(--ink);
  background: var(--accent);
  box-shadow: 3px 3px 0 rgba(23, 25, 26, .22);
}
.fab span { font-size: 1.25rem; }
.bottom-nav {
  position: fixed;
  z-index: 15;
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 5px max(8px, calc((100vw - 780px) / 2)) calc(5px + var(--safe-bottom));
  border-top: 1px solid var(--ink);
  background: rgba(247, 247, 243, .97);
}
.nav-item { position: relative; min-height: 54px; }
.nav-item span, .nav-item small { display: block; }
.nav-item span { font-size: 1.1rem; }
.nav-item small { margin-top: 2px; }
.nav-item.is-active::after { content: ""; position: absolute; right: 28%; bottom: 0; left: 28%; height: 3px; background: var(--accent-dark); }

.modal-backdrop {
  position: fixed;
  z-index: 100;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) 16px max(16px, env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  background: rgba(18, 20, 20, .7);
}
.modal {
  width: min(100%, 430px);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 20px;
  border: 1px solid var(--ink);
  border-top: 6px solid var(--accent);
  background: var(--surface);
  box-shadow: 8px 8px 0 rgba(0,0,0,.25);
}
.modal h2 { margin: 4px 0 12px; font-size: 1.2rem; }
.modal p { color: var(--ink-soft); line-height: 1.6; white-space: pre-line; overflow-wrap: anywhere; }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 18px; }

.toast {
  position: fixed;
  z-index: 120;
  right: 16px;
  bottom: calc(78px + var(--safe-bottom));
  left: 16px;
  width: fit-content;
  max-width: min(420px, calc(100% - 32px));
  margin: auto;
  padding: 11px 15px;
  border-left: 5px solid var(--accent);
  background: var(--ink);
  color: white;
  box-shadow: 3px 3px 0 rgba(0,0,0,.25);
  font-size: 0.88rem;
}

@media (min-width: 640px) {
  .app-shell { padding-right: 24px; padding-left: 24px; }
  .folder-shortcuts { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .filter-grid { grid-template-columns: repeat(2, minmax(0, 240px)); }
  .record-thumb { width: 120px; }
}

@media (max-width: 370px) {
  .app-shell { padding-right: 10px; padding-left: 10px; }
  .filter-grid { grid-template-columns: 1fr; }
  .brand-index { display: none; }
  .status-chip { max-width: 82px; }
  .record-thumb { width: 72px; }
  .folder-actions button { min-width: 38px; padding: 2px; }
  .view-intro { margin-left: 0; }
}

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: toast-in .16s ease-out; }
  @keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }
}

@media (prefers-reduced-motion: reduce) {
  .startup-screen { transition-duration: 10ms; }
  .startup-code,
  .startup-line,
  .startup-line::after,
  .startup-title,
  .startup-status {
    animation: none;
  }
  .startup-code,
  .startup-title,
  .startup-status { opacity: 1; transform: none; }
  .startup-line { transform: scaleX(1); }
  .startup-line::after { display: none; }
}
