:root {
  --paper: #f4efe6;
  --paper-2: #ece5d8;
  --card: #fbf8f1;
  --ink: #20211d;
  --ink-soft: #4a4b42;
  --ink-faint: #8a8a7d;
  --line: #d8cfbe;
  --line-soft: #e4ddcf;
  --accent: #9a3b2e;
  --accent-soft: #c0533f;
  --gold: #b08338;
  --green: #5a7a52;
  --shadow: 0 1px 2px rgba(40, 33, 20, .06), 0 8px 24px rgba(40, 33, 20, .07);
  --shadow-lg: 0 20px 60px rgba(40, 33, 20, .18);
  --radius: 4px;
  --serif: "Fraunces", Georgia, serif;
  --sans: "IBM Plex Sans", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: var(--sans);
  background: var(--paper);
  color: var(--ink);
  display: flex;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  position: relative;
}

/* paper grain overlay */
.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: .035;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 252px;
  flex-shrink: 0;
  background: var(--ink);
  color: var(--paper);
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 46px; }
.brand-mark {
  font-size: 30px;
  color: var(--gold);
  line-height: 1;
  transform: translateY(-1px);
}
.brand-text { display: flex; flex-direction: column; }
.brand-name {
  font-family: var(--mono);
  font-weight: 600;
  letter-spacing: .22em;
  font-size: 15px;
}
.brand-sub {
  font-size: 10px;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-top: 3px;
}

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: transparent;
  border: none;
  color: #b9b4a4;
  font-family: var(--sans);
  font-size: 14px;
  text-align: left;
  padding: 11px 13px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all .18s ease;
  letter-spacing: .01em;
}
.nav-item .nav-glyph { font-size: 14px; width: 16px; text-align: center; opacity: .7; }
.nav-item:hover { background: rgba(255,255,255,.05); color: var(--paper); }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item.active .nav-glyph { opacity: 1; }

.sidebar-foot { margin-top: auto; padding-top: 24px; border-top: 1px solid rgba(255,255,255,.08); }
.foot-stat { font-family: var(--mono); font-size: 12px; color: #b9b4a4; }
.foot-stat span { color: var(--gold); font-weight: 600; }
.foot-engine { font-size: 10px; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-faint); margin-top: 6px; }

/* ---------- Stage ---------- */
.stage { flex: 1; padding: 44px 52px; max-width: 1180px; }
.view.hidden { display: none; }
.view { animation: rise .4s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.view-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 34px;
  padding-bottom: 22px;
  border-bottom: 1.5px solid var(--ink);
}
.view-title {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 40px;
  letter-spacing: -.02em;
  line-height: 1;
}
.view-desc { color: var(--ink-soft); font-size: 14px; margin-top: 9px; }

/* ---------- Buttons ---------- */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  font-family: var(--sans);
  font-weight: 500;
  font-size: 13.5px;
  padding: 11px 18px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all .18s ease;
  white-space: nowrap;
  box-shadow: var(--shadow);
}
.btn-primary:hover { background: var(--accent-soft); transform: translateY(-1px); }
.btn-ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  padding: 9px 15px;
  border-radius: var(--radius);
  font-family: var(--sans);
  font-size: 13px;
  cursor: pointer;
  transition: all .15s ease;
}
.btn-ghost:hover { border-color: var(--ink); color: var(--ink); }

/* ---------- Stat cards ---------- */
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; margin-bottom: 30px; }
.stat-card {
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 24px 26px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}
.stat-card::before {
  content: "";
  position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
  background: var(--gold);
}
.stat-num { font-family: var(--serif); font-size: 46px; font-weight: 600; line-height: 1; letter-spacing: -.02em; }
.stat-label { font-family: var(--mono); font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--ink-faint); margin-top: 10px; }

/* ---------- Dashboard grid ---------- */
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.panel {
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 24px 26px;
  box-shadow: var(--shadow);
}
.panel-title {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line-soft);
}

.dist-chart { display: flex; flex-direction: column; gap: 13px; }
.dist-row { display: flex; align-items: center; gap: 12px; }
.dist-name { font-size: 13px; width: 110px; flex-shrink: 0; color: var(--ink-soft); }
.dist-bar-track { flex: 1; height: 9px; background: var(--paper-2); border-radius: 5px; overflow: hidden; }
.dist-bar { height: 100%; border-radius: 5px; transition: width .6s cubic-bezier(.2,.8,.2,1); }
.dist-count { font-family: var(--mono); font-size: 12px; width: 26px; text-align: right; color: var(--ink); }

.recent-list { display: flex; flex-direction: column; }
.recent-item { display: flex; align-items: baseline; gap: 12px; padding: 11px 0; border-bottom: 1px dotted var(--line); }
.recent-item:last-child { border-bottom: none; }
.recent-name { font-size: 14px; font-weight: 500; flex: 1; }
.recent-code { font-family: var(--mono); font-size: 11px; color: var(--ink-faint); }
.empty-note { color: var(--ink-faint); font-size: 13px; font-style: italic; padding: 8px 0; }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
.search-wrap { flex: 1; min-width: 240px; position: relative; }
.search-glyph { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--ink-faint); font-size: 16px; }
#search-input {
  width: 100%;
  padding: 12px 14px 12px 40px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  font-family: var(--sans);
  font-size: 14px;
  color: var(--ink);
}
#search-input:focus { outline: none; border-color: var(--accent); }
.filter-select {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  font-family: var(--sans);
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  min-width: 150px;
}
.filter-select:focus { outline: none; border-color: var(--accent); }

/* ---------- Document table ---------- */
.doc-table { display: flex; flex-direction: column; }
.doc-row {
  display: grid;
  grid-template-columns: 1fr 150px 120px 110px 90px;
  gap: 16px;
  align-items: center;
  padding: 16px 18px;
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  margin-bottom: 8px;
  box-shadow: var(--shadow);
  transition: all .15s ease;
}
.doc-row:hover { border-color: var(--line); transform: translateX(2px); }
.doc-main { min-width: 0; }
.doc-name { font-family: var(--serif); font-size: 17px; font-weight: 500; margin-bottom: 3px; }
.doc-desc { font-size: 12.5px; color: var(--ink-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-tags { display: flex; gap: 5px; margin-top: 6px; flex-wrap: wrap; }
.tag { font-family: var(--mono); font-size: 10px; background: var(--paper-2); color: var(--ink-soft); padding: 2px 7px; border-radius: 3px; letter-spacing: .03em; }

.cat-chip { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; }
.cat-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.cat-parent { font-size: 10.5px; color: var(--ink-faint); display: block; margin-top: 1px; font-family: var(--mono); letter-spacing: .04em; }

.shelf-code { font-family: var(--mono); font-size: 13px; font-weight: 500; color: var(--ink); background: var(--paper-2); padding: 4px 9px; border-radius: 3px; display: inline-block; }
.doc-date { font-family: var(--mono); font-size: 12px; color: var(--ink-soft); }
.doc-actions { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-faint);
  width: 30px; height: 30px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all .15s ease;
}
.icon-btn:hover { border-color: var(--ink); color: var(--ink); }
.icon-btn.danger:hover { border-color: var(--accent); color: var(--accent); background: rgba(154,59,46,.06); }

.col-head {
  display: grid;
  grid-template-columns: 1fr 150px 120px 110px 90px;
  gap: 16px;
  padding: 0 18px 10px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.col-head span:last-child { text-align: right; }

/* ---------- Classification tree ---------- */
.cat-tree { display: flex; flex-direction: column; gap: 16px; }
.cat-group {
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.cat-parent-row {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 20px;
  border-left: 4px solid var(--gold);
}
.cat-parent-name { font-family: var(--serif); font-size: 19px; font-weight: 600; flex: 1; }
.cat-children { padding: 4px 20px 14px 36px; display: flex; flex-wrap: wrap; gap: 8px; }
.child-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--paper-2);
  border: 1px solid var(--line-soft);
  padding: 6px 10px 6px 12px;
  border-radius: 20px;
  font-size: 13px;
}
.child-chip .del-x {
  cursor: pointer; color: var(--ink-faint); font-size: 12px;
  width: 16px; height: 16px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%;
}
.child-chip .del-x:hover { background: var(--accent); color: #fff; }

/* ---------- Shelves ---------- */
.shelf-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.shelf-card {
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  position: relative;
}
.shelf-card-code { font-family: var(--mono); font-size: 22px; font-weight: 600; letter-spacing: .04em; color: var(--accent); }
.shelf-meta { font-size: 13px; color: var(--ink-soft); margin-top: 12px; line-height: 1.7; }
.shelf-meta b { color: var(--ink); font-weight: 600; }
.shelf-notes { font-size: 12.5px; color: var(--ink-faint); font-style: italic; margin-top: 10px; }
.shelf-count {
  position: absolute; top: 18px; right: 18px;
  font-family: var(--mono); font-size: 11px;
  background: var(--ink); color: var(--paper);
  padding: 4px 9px; border-radius: 3px;
}
.shelf-del { margin-top: 16px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(32, 33, 29, .55);
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  animation: fade .2s ease;
}
.modal-overlay.hidden { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--paper);
  border-radius: 6px;
  width: 540px;
  max-width: calc(100vw - 40px);
  max-height: calc(100vh - 60px);
  overflow: auto;
  box-shadow: var(--shadow-lg);
  animation: pop .25s cubic-bezier(.2,.9,.3,1);
}
@keyframes pop { from { opacity: 0; transform: scale(.96) translateY(10px); } to { opacity: 1; transform: none; } }
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 22px 26px;
  border-bottom: 1.5px solid var(--ink);
}
.modal-head h2 { font-family: var(--serif); font-size: 24px; font-weight: 600; }
.modal-close { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--ink-faint); transition: color .15s; }
.modal-close:hover { color: var(--accent); }
.modal-body { padding: 26px; }

.field { margin-bottom: 18px; }
.field label {
  display: block;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 7px;
}
.field input, .field textarea, .field select {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  font-family: var(--sans);
  font-size: 14px;
  color: var(--ink);
}
.field textarea { resize: vertical; min-height: 72px; }
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--accent); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field-hint { font-size: 11px; color: var(--ink-faint); margin-top: 5px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 26px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--paper);
  padding: 13px 22px; border-radius: var(--radius);
  font-size: 13.5px; z-index: 2000;
  box-shadow: var(--shadow-lg);
  animation: toastIn .3s ease;
}
.toast.hidden { display: none; }
.toast.error { background: var(--accent); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }

.empty-state { text-align: center; padding: 70px 20px; color: var(--ink-faint); }
.empty-state .es-glyph { font-size: 48px; opacity: .25; display: block; margin-bottom: 16px; }
.empty-state .es-text { font-family: var(--serif); font-size: 19px; font-style: italic; }

@media (max-width: 880px) {
  .sidebar { width: 70px; padding: 24px 12px; }
  .brand-text, .nav-item span:not(.nav-glyph), .sidebar-foot { display: none; }
  .nav-item { justify-content: center; }
  .stage { padding: 28px 24px; }
  .dash-grid, .stat-row, .field-row { grid-template-columns: 1fr; }
  .doc-row, .col-head { grid-template-columns: 1fr auto; }
  .doc-row > :nth-child(3), .doc-row > :nth-child(4), .col-head > :nth-child(3), .col-head > :nth-child(4) { display: none; }
}

/* ---------- Attachments ---------- */
.attach-badge {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent);
  background: rgba(154, 59, 46, .08);
  border: 1px solid rgba(154, 59, 46, .2);
  padding: 1px 7px;
  border-radius: 20px;
  vertical-align: middle;
  margin-left: 6px;
  white-space: nowrap;
}

#attach-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.attach-empty { font-size: 12.5px; color: var(--ink-faint); font-style: italic; margin-bottom: 14px; }
.attach-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
}
.attach-icon { font-size: 18px; color: var(--ink-soft); flex-shrink: 0; }
.attach-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.attach-name {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attach-name:hover { color: var(--accent); text-decoration: underline; }
.attach-sub { font-family: var(--mono); font-size: 10.5px; color: var(--ink-faint); }
a.icon-btn { text-decoration: none; }

.upload-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 22px 16px;
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
  background: var(--paper-2);
  cursor: pointer;
  transition: all .18s ease;
  text-align: center;
}
.upload-drop:hover { border-color: var(--accent); background: rgba(154, 59, 46, .04); }
.upload-drop.dragging { border-color: var(--accent); background: rgba(154, 59, 46, .09); transform: scale(1.01); }
.upload-drop.uploading { opacity: .6; pointer-events: none; }
.upload-glyph { font-size: 26px; color: var(--ink-faint); line-height: 1; }
.upload-text { font-size: 13.5px; color: var(--ink-soft); }
.upload-text b { color: var(--accent); }
.upload-hint { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-faint); }
