:root {
  --bg: #faf3e6;
  --surface: #fffdf8;
  --ink: #1f1a16;
  --muted: #7b6656;
  --line: #ecdcc0;
  --rust: #b5462a;
  --rust-dark: #8f3520;
  --rust-soft: #f8e3d8;
  --green: #2a6e4a;
  --green-soft: #e1f0e6;
  --danger: #b42318;
  --danger-soft: #fbe3e0;
  --amber: #8a5a00;
  --amber-soft: #fdf0c4;
  --blue: #1f4e9c;
  --blue-soft: #e3ecfa;
  --purple: #5b3fa0;
  --purple-soft: #ece6f7;
  --radius: 14px;
  --script: "Caveat Brush", "Segoe Script", cursive;
  --nav-h: 64px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.5 "Plus Jakarta Sans", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--rust); }
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -.01em; }
h1 { font-size: 22px; font-weight: 800; }
h2 { font-size: 17px; font-weight: 700; }
h3 { font-size: 15px; font-weight: 700; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- Shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); }
.brand .logo { font-family: var(--script); color: var(--rust); font-size: 22px; line-height: .8; }
.brand .logo span { display: block; margin-left: 10px; }
.brand .app { font-size: 11px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.who { text-align: right; font-size: 12px; line-height: 1.3; }
.who b { display: block; font-size: 13px; }
.top-right { display: flex; align-items: center; gap: 12px; }
.logout-btn {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px;
  border: 1.5px solid var(--line); background: #fff; color: var(--ink); border-radius: 10px;
  padding: 6px 10px; font: inherit; font-size: 13px; font-weight: 700; cursor: pointer;
}
.logout-btn svg { width: 18px; height: 18px; }
.logout-btn:hover { border-color: var(--danger); color: var(--danger); }
.who b, .who span { display: block; max-width: 38vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 400px) { .brand .app { display: none; } }

.layout { display: block; }
main { padding: 16px 16px calc(var(--nav-h) + 24px); max-width: 1100px; margin: 0 auto; }

.nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
  height: var(--nav-h); padding-bottom: env(safe-area-inset-bottom);
}
.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 600;
}
.nav a svg { width: 22px; height: 22px; }
.nav a.active { color: var(--rust); }
.nav a.create .ic {
  width: 44px; height: 44px; border-radius: 50%; background: var(--rust); color: #fff;
  display: grid; place-items: center; margin-top: -18px; box-shadow: 0 6px 16px -6px rgba(181, 70, 42, .7);
}
.nav a.create .ic svg { width: 24px; height: 24px; }
.nav .nav-extra { display: none; }

@media (min-width: 900px) {
  .layout { display: grid; grid-template-columns: 220px 1fr; }
  .nav {
    position: sticky; top: 57px; height: calc(100vh - 57px); flex-direction: column;
    border-top: 0; border-right: 1px solid var(--line); padding: 16px 10px; gap: 2px;
  }
  .nav a { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
  .nav a:hover { background: var(--bg); }
  .nav a.active { background: var(--rust-soft); }
  .nav a.create .ic { width: auto; height: auto; margin: 0; background: none; color: inherit; box-shadow: none; }
  .nav a.create .ic svg { width: 22px; height: 22px; }
  .nav .nav-extra { display: flex; }
  .nav .nav-mobile-only { display: none; }
  .nav .sep { height: 1px; background: var(--line); margin: 10px 8px; }
  main { padding: 24px 28px 48px; margin: 0; }
}

/* ---------- Common blocks ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card + .card, .stack > * + * { margin-top: 12px; }
.grid > .card, .po-list > .card { margin-top: 0; }
.grid { display: grid; gap: 12px; }
@media (min-width: 640px) { .grid.cols-2 { grid-template-columns: 1fr 1fr; } .grid.cols-3 { grid-template-columns: repeat(3, 1fr); } .grid.cols-4 { grid-template-columns: repeat(4, 1fr); } }
.section-title { display: flex; justify-content: space-between; align-items: baseline; margin: 22px 0 10px; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }

.stat .label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.stat .value { font-size: 24px; font-weight: 800; letter-spacing: -.02em; margin-top: 4px; }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
a.stat { text-decoration: none; color: inherit; display: block; }
a.stat:hover { border-color: var(--rust); }

.bars { display: grid; gap: 10px; margin-top: 12px; }
.bar-row { display: grid; grid-template-columns: 92px 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.bar-track { height: 10px; background: var(--bg); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--rust); border-radius: 99px; }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; white-space: nowrap;
  font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 99px; letter-spacing: .02em;
}
.st-PENDING_APPROVAL { background: var(--amber-soft); color: var(--amber); }
.st-APPROVED { background: var(--blue-soft); color: var(--blue); }
.st-ORDERED { background: var(--purple-soft); color: var(--purple); }
.st-COMPLETED { background: var(--green-soft); color: var(--green); }
.st-REJECTED { background: var(--danger-soft); color: var(--danger); }
.b-urgent { background: var(--danger); color: #fff; }
.b-hike { background: var(--danger-soft); color: var(--danger); }
.b-div { background: var(--bg); color: var(--ink); border: 1px solid var(--line); }
.b-ok { background: var(--green-soft); color: var(--green); }
.b-lock { background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.b-new { background: var(--green); color: #fff; }
.item-row.is-new { border-color: var(--green); border-style: dashed; }
.picker-list button.picker-new { background: var(--green-soft); color: var(--green); border-bottom: 0; }
.picker-list button.picker-new:hover, .picker-list button.picker-new:focus { background: #cfe6d7; }
.picker-list button.picker-new small { color: var(--green); }

/* ---------- PO list ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 7px 14px; border-radius: 99px; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; text-decoration: none;
}
.chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.po-list { display: grid; gap: 10px; }
.po-card { display: block; text-decoration: none; color: inherit; }
.po-card:hover { border-color: var(--rust); }
.po-card .top { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.po-card .no { font-weight: 700; }
.po-card .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.po-card .bottom { display: flex; justify-content: space-between; align-items: flex-end; margin-top: 10px; font-size: 13px; }
.po-card .amount { font-weight: 800; font-size: 16px; text-align: right; }

/* ---------- Forms ---------- */
label.f { display: grid; gap: 5px; font-size: 13px; font-weight: 600; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  font: inherit; color: var(--ink); background: #fff; width: 100%;
  border: 1.5px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 44px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--rust); box-shadow: 0 0 0 3px rgba(181, 70, 42, .15); }
input[type=number] { -moz-appearance: textfield; }
input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; }
.form-grid { display: grid; gap: 14px; }
@media (min-width: 640px) { .form-grid.two { grid-template-columns: 1fr 1fr; } }
.check { display: flex; gap: 8px; align-items: center; font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--rust); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 700; font-size: 14px; text-decoration: none; cursor: pointer;
  border-radius: 10px; padding: 10px 16px; min-height: 44px; border: 1.5px solid transparent;
  background: var(--rust); color: #fff; white-space: nowrap;
}
.btn:hover { background: var(--rust-dark); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn.block { width: 100%; }
.btn.green { background: var(--green); }
.btn.green:hover { filter: brightness(1.1); background: var(--green); }
.btn.danger { background: var(--danger); }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: var(--bg); }
.btn.sm { min-height: 34px; padding: 6px 12px; font-size: 13px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1.5px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label { text-align: center; padding: 10px; font-weight: 700; font-size: 14px; cursor: pointer; background: #fff; }
.seg label:not(:first-of-type) { border-left: 1.5px solid var(--line); }
.seg input:checked + label { background: var(--ink); color: #fff; }
.seg input:checked + label.urgent { background: var(--danger); }
.seg input:focus-visible + label { outline: 2px solid var(--rust); outline-offset: -2px; }

/* ---------- Item rows (create / receive) ---------- */
.item-row { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: #fff; }
.item-row + .item-row { margin-top: 10px; }
.item-row .head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.item-row .name { font-weight: 700; }
.item-row .fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.item-row .fields.three { grid-template-columns: 1fr 1fr; }
.item-row .fields .full { grid-column: 1 / -1; }
.item-row .sub { display: flex; justify-content: space-between; margin-top: 8px; font-size: 13px; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 4px; border-radius: 8px; font-size: 18px; line-height: 1; }
.icon-btn:hover { background: var(--bg); color: var(--danger); }

.picker { position: relative; }
.picker-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 15;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; max-height: 300px; overflow-y: auto;
  box-shadow: 0 12px 32px -12px rgba(0, 0, 0, .25);
}
.picker-list button {
  display: flex; justify-content: space-between; gap: 8px; width: 100%; text-align: left;
  border: 0; background: none; font: inherit; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line);
}
.picker-list button:hover, .picker-list button:focus { background: var(--rust-soft); outline: none; }
.picker-list button small { color: var(--muted); }

.total-bar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-radius: 12px; background: var(--ink); color: #fff; font-weight: 700;
}
.total-bar .v { font-size: 20px; }

/* ---------- Photo ---------- */
.photo-input { display: grid; gap: 8px; }
.photo-input input { display: none; }
.photo-drop {
  display: flex; align-items: center; justify-content: center; gap: 8px; text-align: center;
  border: 1.5px dashed var(--rust); border-radius: 12px; padding: 16px; color: var(--rust); font-weight: 700; cursor: pointer; background: #fff;
}
.photo-preview { max-width: 100%; max-height: 240px; border-radius: 10px; border: 1px solid var(--line); display: block; }
.thumb { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); cursor: zoom-in; display: block; }

/* ---------- Detail ---------- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }
.items-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.items-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); }
.items-table td { padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.items-table .r { text-align: right; }
.table-wrap { overflow-x: auto; }
.item-line { padding: 12px 0; border-bottom: 1px solid var(--line); display: grid; gap: 3px; }
.item-line:last-child { border-bottom: 0; }
.item-line .head { display: flex; justify-content: space-between; gap: 12px; }
.items-table-wrap { display: none; }
@media (min-width: 640px) { .item-cards { display: none; } .items-table-wrap { display: block; } }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 14px 22px; font-size: 14px; }
.timeline li::before { content: ""; position: absolute; left: 5px; top: 7px; width: 8px; height: 8px; border-radius: 50%; background: var(--rust); }
.timeline li::after { content: ""; position: absolute; left: 8.5px; top: 18px; bottom: 0; width: 1px; background: var(--line); }
.timeline li:last-child::after { display: none; }
.timeline .when { font-size: 12px; color: var(--muted); }
.action-card { border: 2px solid var(--rust); }
.notice { padding: 12px 14px; border-radius: 10px; font-size: 14px; }
.notice.red { background: var(--danger-soft); color: var(--danger); }
.notice.amber { background: var(--amber-soft); color: var(--amber); }
.notice.green { background: var(--green-soft); color: var(--green); }
.hike-text { color: var(--danger); font-weight: 700; font-size: 12px; }

/* ---------- Ledger ---------- */
.filters { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
@media (min-width: 900px) { .filters { grid-template-columns: repeat(6, 1fr); align-items: end; } }
.ledger { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 760px; }
.ledger th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 10px 8px; border-bottom: 1px solid var(--line); background: var(--surface); position: sticky; top: 0; }
.ledger td { padding: 10px 8px; border-bottom: 1px solid var(--line); }
.ledger tr:hover td { background: #fff; }
.ledger .r { text-align: right; }
.up { color: var(--danger); }
.down { color: var(--green); }

/* ---------- Master tables ---------- */
.import-hint { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; background: var(--green-soft); border-color: #cfe6d7; }
.import-hint .btn-row > .btn { flex: none; }
.list-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.list-table td, .list-table th { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: left; }
.list-table th { font-size: 12px; color: var(--muted); font-weight: 600; }
.list-table tr.inactive td { color: var(--muted); }
.list-table .r { text-align: right; }

/* ---------- Modal & toast ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 50; background: rgba(31, 26, 22, .5);
  display: flex; align-items: flex-end; justify-content: center;
}
@media (min-width: 640px) { .modal-back { align-items: center; padding: 20px; } }
.modal {
  background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
}
@media (min-width: 640px) { .modal { border-radius: 18px; padding: 24px; } }
.modal .modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal.image { max-width: 900px; padding: 12px; background: #111; }
.modal.image img { width: 100%; height: auto; display: block; border-radius: 8px; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 16px); transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 600; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: 0 10px 28px -10px rgba(0, 0, 0, .4);
}
.toast.error { background: var(--danger); }
@media (min-width: 900px) { .toast { bottom: 24px; } }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: var(--rust); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px; padding: 28px 24px; }
.login-card .logo { font-family: var(--script); color: var(--rust); font-size: 40px; line-height: .8; }
.login-card .logo span { display: block; margin-left: 18px; }
.login-card .app { margin: 14px 0 22px; font-size: 12px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.spinner { display: grid; place-items: center; padding: 48px; color: var(--muted); }
