:root {
  --ink: #2c1c12;
  --muted: #6d5746;
  --line: #e4d4bc;
  --paper: #f7f1e8;
  --stone: #f3ebe0;
  --wine: #6b3e26;
  --wine-deep: #542f1c;
  --brass: #c4a574;
  --brass-soft: rgba(196, 165, 116, 0.28);
  --accent: #6b3e26;
  --white: #fffdf8;
  --shadow: 0 10px 28px rgba(44, 28, 18, 0.08);
  --radius: 12px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  overflow-x: hidden;
  min-height: 0;
  align-items: start;
}
body {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  background:
    radial-gradient(ellipse 80% 40% at 100% 0%, rgba(196, 165, 116, 0.16), transparent 50%),
    var(--paper);
  color: var(--ink);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
#login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background:
    radial-gradient(ellipse 70% 50% at 80% 8%, rgba(107, 62, 38, 0.1), transparent 50%),
    var(--stone);
}
#login[hidden], #app[hidden] { display: none !important; }
.card {
  background: var(--white);
  padding: 36px 30px;
  width: min(400px, 100%);
  border: 1px solid var(--brass);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card img { width: 48px; height: 48px; }
.card h1 {
  font-family: "Iowan Old Style", Palatino, "Times New Roman", serif;
  font-size: 24px;
  font-weight: 400;
  letter-spacing: 0.04em;
  margin: 14px 0 22px;
  color: var(--wine);
}
label { display: block; font-size: 12px; margin: 12px 0 6px; color: var(--wine); letter-spacing: 0.08em; text-transform: uppercase; }
label.check { text-transform: none; letter-spacing: 0; font-size: 14px; color: var(--ink); display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 14px 0; }
label.check input[type="checkbox"] { width: auto; }
input, textarea, select {
  width: 100%;
  max-width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--brass);
  border-radius: 8px;
  font: inherit;
  font-size: 16px;
  background: var(--white);
  color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s;
}
input::placeholder, textarea::placeholder { color: #b7a28c; }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--wine);
  box-shadow: 0 0 0 3px rgba(196, 165, 116, 0.38);
}
.field-hint { display: block; font-size: 12px; color: var(--muted); margin-top: 6px; letter-spacing: 0; text-transform: none; }
button { font: inherit; cursor: pointer; }
.card button, .primary {
  margin-top: 16px;
  background: var(--wine);
  color: #fff;
  border: 0;
  padding: 12px 18px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 12px;
  border-radius: 8px;
}
.card button:hover, .primary:hover { background: var(--wine-deep); }
.err { color: #a33; font-size: 13px; margin-top: 10px; min-height: 1em; }

#app {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  align-items: start;
  align-content: start;
  min-height: 100dvh;
  min-width: 0;
  width: 100%;
}
aside {
  position: sticky;
  top: 0;
  z-index: 30;
  height: 100dvh;
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--white);
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-self: start;
}
.aside-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.aside-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.aside-brand img { width: 36px; height: 36px; flex: 0 0 auto; }
.aside-brand strong {
  display: block;
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--wine);
  font-weight: 500;
}
#aside-role {
  display: block;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
  margin-top: 2px;
}
.nav-toggle {
  display: none;
  width: 42px;
  height: 42px;
  margin: 0;
  padding: 0;
  border: 1px solid var(--brass);
  background: var(--stone);
  color: var(--wine);
  border-radius: 8px;
  place-items: center;
  flex: 0 0 auto;
}
.nav-toggle span,
.nav-toggle span::before,
.nav-toggle span::after {
  display: block;
  width: 16px;
  height: 1.5px;
  background: currentColor;
  position: relative;
}
.nav-toggle span::before,
.nav-toggle span::after { content: ""; position: absolute; left: 0; }
.nav-toggle span::before { top: -5px; }
.nav-toggle span::after { top: 5px; }
.nav-toggle[aria-expanded="true"] span { background: transparent; }
.nav-toggle[aria-expanded="true"] span::before { top: 0; transform: rotate(45deg); }
.nav-toggle[aria-expanded="true"] span::after { top: 0; transform: rotate(-45deg); }
aside nav { display: flex; flex-direction: column; gap: 4px; }
aside nav button, #logout {
  text-align: left;
  background: none;
  border: 0;
  padding: 10px 12px;
  color: var(--muted);
  min-height: 40px;
  border-radius: 8px;
}
aside nav button.active, aside nav button:hover {
  background: var(--stone);
  color: var(--wine);
}
#logout { margin-top: auto; color: var(--wine); }

main {
  padding: 20px 28px 40px;
  min-width: 0;
  width: 100%;
  align-self: start;
}
h2 {
  font-family: "Iowan Old Style", Palatino, "Times New Roman", serif;
  font-size: 28px;
  font-weight: 400;
  margin-bottom: 6px;
  color: var(--wine);
}
.sub { color: var(--muted); margin-bottom: 22px; font-size: 14px; line-height: 1.5; }
.view-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 22px;
}
.view-head .sub { margin-bottom: 0; }
.view-head .add-new { margin-top: 0; flex: 0 0 auto; white-space: nowrap; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
.stat {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 16px;
  min-width: 0;
  box-shadow: 0 6px 18px rgba(44, 28, 18, 0.04);
}
.stat b { display: block; font-size: 26px; color: var(--wine); letter-spacing: -0.02em; }
.stat span { font-size: 12px; color: var(--muted); letter-spacing: 0.04em; }

.panel {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 18px 14px;
  margin-bottom: 16px;
  box-shadow: 0 6px 18px rgba(44, 28, 18, 0.04);
}
.panel h3 { margin-top: 0; }
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.dash-grid .panel { margin-bottom: 0; }

.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: transparent; font-size: 14px; }
th, td { border-bottom: 1px solid var(--line); text-align: left; padding: 12px 8px; vertical-align: top; }
th { color: var(--wine); font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; font-weight: 500; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.row-actions button, .ghost {
  background: none;
  border: 1px solid var(--brass);
  color: var(--wine);
  padding: 8px 12px;
  border-radius: 8px;
}
.row-actions button:hover, .ghost:hover { background: var(--stone); }
.ghost { display: inline-block; text-decoration: none; }

form.stack {
  max-width: 640px;
  width: 100%;
  background:
    linear-gradient(180deg, #fffefb 0%, #faf4ea 100%);
  border: 1px solid var(--brass);
  border-radius: 14px;
  padding: 22px 22px 18px;
  margin-bottom: 20px;
  box-shadow:
    0 0 0 1px rgba(255, 253, 248, 0.7) inset,
    0 10px 28px rgba(44, 28, 18, 0.06);
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.msg {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  overflow-wrap: anywhere;
}
.thread { font-size: 13px; color: var(--muted); white-space: pre-wrap; margin: 8px 0; }
.qr img { width: min(240px, 100%); background: #fff; padding: 10px; border: 1px solid var(--brass); border-radius: 8px; }
.ok { color: #2a6; font-size: 13px; margin-top: 8px; }
h3 { font-family: "Iowan Old Style", Palatino, serif; font-weight: 400; color: var(--wine); margin: 6px 0 12px; font-size: 20px; }
.bar-row {
  display: grid;
  grid-template-columns: minmax(90px, 180px) 1fr 48px;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
  font-size: 13px;
}
.bar-row span { color: var(--muted); overflow-wrap: anywhere; }
.bar { height: 8px; background: var(--stone); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--wine); width: 0; border-radius: 99px; }
.source-split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.source-split > div { border: 1px solid var(--line); background: var(--stone); border-radius: 10px; padding: 14px; }
.source-split span { font-size: 12px; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; }
.source-split b { display: block; font-size: 22px; color: var(--wine); margin: 4px 0 8px; }

#toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--wine);
  color: #fff;
  padding: 12px 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 80;
  letter-spacing: 0.04em;
  font-size: 13px;
  border-radius: 8px;
  max-width: min(360px, calc(100% - 32px));
}
#toast[hidden] { display: none; }
#toast.show { display: block; opacity: 1; }
#toast.err { background: #a33; }
code { color: var(--wine); font-size: 13px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--stone);
  border: 1px solid var(--brass-soft);
  color: var(--wine);
  padding: 4px 9px;
  font-size: 12px;
  letter-spacing: 0.04em;
  border-radius: 99px;
}
.chip.muted { color: var(--muted); border-style: dashed; }
.chip-x {
  border: 0;
  background: none;
  color: var(--wine);
  padding: 0 2px;
  margin: 0;
  font-size: 16px;
  line-height: 1;
  min-height: 0;
}
.tag-field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-height: 46px;
  padding: 6px 8px;
  border: 1px solid var(--brass);
  border-radius: 8px;
  background: var(--white);
}
.tag-field:focus-within {
  border-color: var(--wine);
  box-shadow: 0 0 0 3px rgba(196, 165, 116, 0.38);
}
.tag-list { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-input {
  flex: 1 1 140px;
  border: 0 !important;
  box-shadow: none !important;
  padding: 6px 4px !important;
  min-width: 120px;
  width: auto;
}

.disc-list { display: flex; flex-direction: column; gap: 8px; margin: 8px 0 4px; }
.disc-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--stone);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.disc-row input[type="checkbox"] { width: auto; }
.disc-name { font-size: 14px; color: var(--ink); letter-spacing: 0; text-transform: none; }
.pct {
  display: inline-flex;
  align-items: center;
  background: var(--white);
  border: 1px solid var(--brass);
  border-radius: 8px;
  overflow: hidden;
}
.pct input[type="number"] {
  width: 64px;
  border: 0 !important;
  box-shadow: none !important;
  border-radius: 0;
  padding: 8px 8px;
  text-align: right;
}
.pct em {
  font-style: normal;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--wine);
  padding: 0 10px 0 4px;
  background: var(--stone);
  align-self: stretch;
  display: grid;
  place-items: center;
}

#modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 28px 16px;
  isolation: isolate;
}
#modal[hidden] { display: none !important; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(ellipse 90% 70% at 50% 42%, rgba(84, 47, 28, 0.22), transparent 62%),
    rgba(22, 12, 8, 0.52);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
  pointer-events: auto;
}
.modal-panel {
  position: relative;
  z-index: 1;
  width: min(580px, 100%);
  max-height: min(92vh, 860px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background-color: #fffefb;
  background-image: linear-gradient(180deg, #fffefb 0%, #f8f0e4 100%);
  border: 1px solid var(--brass);
  border-radius: 18px;
  padding: 0;
  box-shadow:
    0 0 0 1px rgba(255, 253, 248, 0.55) inset,
    0 28px 70px rgba(22, 12, 8, 0.36);
  animation: modal-in 0.2s ease;
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to { opacity: 1; transform: none; }
}
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin: 0;
  padding: 20px 22px 16px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fffdf8, #faf4ea);
  position: sticky;
  top: 0;
  z-index: 2;
}
.modal-head h3 { margin: 0; font-size: 24px; }
.modal-x {
  width: 36px;
  height: 36px;
  border: 1px solid var(--brass);
  background: var(--stone);
  color: var(--wine);
  font-size: 22px;
  line-height: 1;
  padding: 0;
  min-height: 0;
  border-radius: 50%;
  flex: 0 0 auto;
}
.modal-x:hover { background: var(--white); }
.modal-body {
  padding: 8px 22px 20px;
}
.modal-body .stack {
  border: 0;
  padding: 0;
  margin: 0;
  max-width: none;
  background: transparent;
  box-shadow: none;
}
.modal-body label { margin-top: 14px; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, #f8f0e4 72%, rgba(248, 240, 228, 0));
}
.modal-actions .primary,
.modal-actions .ghost { margin-top: 0; }
.modal-body > .err { margin-top: 8px; }
.reply-row { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.reply-row .primary { margin-top: 0; align-self: flex-start; }
body.modal-open { overflow: hidden; }

@media (max-width: 1199px) {
  #app { grid-template-columns: 196px minmax(0, 1fr); }
  main { padding: 18px 20px 36px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .dash-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  #app { grid-template-columns: 1fr; align-content: start; align-items: start; }
  aside {
    position: sticky;
    top: 0;
    z-index: 30;
    height: auto;
    max-height: none;
    overflow: visible;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px 14px;
  }
  .nav-toggle { display: grid; }
  aside nav, #logout { display: none; }
  aside.open nav, aside.open #logout { display: flex; }
  #logout { margin-top: 8px; }
  main { padding: 16px 16px 32px; }
  .grid2, .source-split { grid-template-columns: 1fr; }
  .view-head { flex-direction: column; align-items: stretch; }
  .view-head .add-new { width: 100%; }
  #modal { padding: 0; place-items: end stretch; }
  .modal-panel {
    width: 100%;
    max-height: 96dvh;
    min-height: 72dvh;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
    border-radius: 18px 18px 0 0;
  }
  .grid2, .stats, .source-split { grid-template-columns: 1fr; }
  .disc-row { grid-template-columns: auto 1fr; }
  .disc-row .pct { grid-column: 2; justify-self: start; }
  .bar-row { grid-template-columns: 1fr 48px; }
  .bar-row .bar { grid-column: 1 / -1; }
  input, textarea, select, button { font-size: 16px; }
  form.stack { padding: 16px; }
  table, thead, tbody, th, td, tr { display: block; width: 100%; }
  thead { display: none; }
  tr {
    border: 1px solid var(--line);
    background: var(--white);
    margin-bottom: 12px;
    padding: 12px 14px;
    border-radius: 10px;
  }
  td {
    border: 0;
    padding: 6px 0;
    overflow-wrap: anywhere;
  }
  .row-actions { margin-top: 8px; }
}

@media (max-width: 375px) {
  #login { padding: 16px 12px; }
  .card { padding: 26px 18px; }
  main { padding: 12px 12px 28px; }
  aside { padding: 8px 12px; }
  .stat { padding: 14px 12px; }
  .panel { padding: 14px 12px 12px; }
}
