:root {
  --paper: #f7f6f3;
  --paper-2: #efeee9;
  --card: #ffffff;
  --ink: #16150f;
  --ink-soft: #545045;
  --mute: #8c877b;
  --line: #e0ddd4;
  --accent: #6f5b3e;
  --ok: #3f6b4b;
  --warn: #9a6b1f;
  --bad: #9b3b25;
  --sans: 'Inter', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Instrument Serif', ui-serif, Georgia, serif;
}

@font-face { font-family: 'Inter'; src: url('/fonts/inter.woff2') format('woff2'); font-weight: 300 600; font-display: swap; }
@font-face { font-family: 'Instrument Serif'; src: url('/fonts/instrument-serif.woff2') format('woff2'); font-display: swap; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font-family: var(--sans); font-size: 14px; line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 500; letter-spacing: -.01em; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; }
code { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--paper-2); padding: 1px 5px; border-radius: 3px; }
.muted { color: var(--mute); }

/* login */
.login-view { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: var(--ink); }
.login-card { width: min(400px, 100%); background: var(--paper); padding: 38px 32px 26px; }
.login-brand { font-family: var(--serif); font-size: 30px; letter-spacing: .02em; }
.login-brand span { font-style: italic; color: var(--accent); }
.login-sub { font-size: 11px; letter-spacing: .16em; text-transform: uppercase; color: var(--mute); margin: 2px 0 26px; }
.login-foot { font-size: 11.5px; color: var(--mute); margin: 16px 0 0; text-align: center; }
.login-msg { font-size: 12.5px; color: var(--bad); min-height: 18px; margin: 6px 0; }

.fld { display: block; margin-bottom: 14px; }
.fld > span { display: block; font-size: 10.5px; letter-spacing: .12em; text-transform: uppercase; color: var(--mute); margin-bottom: 6px; }
input[type=text], input[type=email], input[type=tel], input[type=password], input[type=number], input[type=search], textarea, select {
  width: 100%; border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 10px 12px; font: inherit; font-size: 13.5px; min-height: 40px; border-radius: 2px;
}
input:focus, textarea:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 30%, transparent); border-color: var(--accent); }
textarea { resize: vertical; min-height: 90px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 40px; padding: 0 18px; background: var(--ink); color: #fff; border: 1px solid var(--ink);
  font-size: 12px; letter-spacing: .06em; text-transform: uppercase; font-weight: 500;
  transition: opacity .2s, background .2s;
}
.btn:hover { opacity: .86; }
.btn.ghost { background: transparent; color: var(--ink); }
.btn.sm { height: 34px; padding: 0 12px; font-size: 11px; }
.btn.ok { background: var(--ok); border-color: var(--ok); }
.btn.bad { background: var(--bad); border-color: var(--bad); }
.link-btn { background: none; border: 0; padding: 0; font-size: 12.5px; color: var(--mute); text-decoration: underline; text-underline-offset: 3px; }

/* shell */
.app { display: grid; grid-template-columns: 218px 1fr; min-height: 100vh; }
.rail {
  background: var(--ink); color: #d8d3c7; padding: 22px 0;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.rail-brand { font-family: var(--serif); font-size: 25px; color: #f6f3ea; padding: 0 22px 26px; }
.rail-brand span { font-style: italic; color: #b3a184; }
.rail-brand small { font-family: var(--sans); font-size: 9.5px; letter-spacing: .16em; text-transform: uppercase; color: #7d786c; display: block; margin-top: 2px; }
.rail-nav { display: flex; flex-direction: column; gap: 1px; }
.rail-nav button {
  display: flex; align-items: center; gap: 11px; width: 100%;
  background: none; border: 0; padding: 11px 22px; color: #b9b3a6;
  font-size: 13px; text-align: left; transition: background .2s, color .2s;
}
.rail-nav button:hover { background: rgba(255,255,255,.06); color: #fff; }
.rail-nav button.active { background: rgba(255,255,255,.1); color: #fff; }
.rail-nav i { font-style: normal; font-size: 11px; opacity: .7; width: 14px; }
.rail-nav .pill {
  margin-left: auto; background: #b3a184; color: #1a1813; font-size: 10px; font-weight: 600;
  min-width: 18px; height: 18px; line-height: 18px; text-align: center; padding: 0 5px; border-radius: 9px;
}
.rail-nav .pill:empty { display: none; }
.rail-foot { margin-top: auto; padding: 18px 22px 0; display: flex; flex-direction: column; gap: 8px; font-size: 12px; }
.rail-foot a { color: #b3a184; }
.rail-foot #railUser { color: #7d786c; font-size: 11.5px; }

.panel { padding: 0 clamp(18px, 3vw, 40px) 60px; min-width: 0; }
.panel-head {
  position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(10px);
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 18px 0 16px; border-bottom: 1px solid var(--line); margin-bottom: 24px;
}
.panel-head h1 { font-family: var(--serif); font-size: 26px; }
.panel-actions { display: flex; align-items: center; gap: 12px; font-size: 12px; }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 18%, transparent); }

/* stats */
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.stat { background: var(--card); border: 1px solid var(--line); padding: 18px 20px; }
.stat span { font-size: 10.5px; letter-spacing: .13em; text-transform: uppercase; color: var(--mute); }
.stat b { display: block; font-family: var(--serif); font-size: 32px; line-height: 1.15; margin: 6px 0 2px; }
.stat small { font-size: 11.5px; color: var(--mute); }
.stat em { font-style: normal; color: var(--bad); font-weight: 600; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.card { background: var(--card); border: 1px solid var(--line); padding: 20px; }
.card h2 { font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--mute); margin-bottom: 14px; font-weight: 500; }
.card.notice { background: #fffdf6; border-color: #e8ddc2; }
.card.notice h2 { color: var(--warn); }
.card.notice ul { margin: 0; padding-left: 18px; font-size: 13px; color: var(--ink-soft); }
.card.notice li { margin-bottom: 8px; }

.mini-table { display: flex; flex-direction: column; }
.mini-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.mini-row:last-child { border-bottom: 0; }
.mini-row b { font-weight: 500; }
.mini-row span.muted { font-size: 11.5px; }
.mini-empty { color: var(--mute); font-size: 13px; padding: 10px 0; }

/* list / rows */
.toolbar { display: flex; gap: 10px; margin-bottom: 16px; align-items: center; }
.toolbar input { max-width: 320px; }
.list { display: flex; flex-direction: column; gap: 8px; }
.row-item { background: var(--card); border: 1px solid var(--line); padding: 14px 18px; display: grid; gap: 12px; align-items: center; }
.row-item.orders { grid-template-columns: 1.1fr 1.4fr .7fr .8fr auto; }
.row-item.products { grid-template-columns: 48px 1.6fr .7fr .6fr .8fr auto; }
.row-item.leads { grid-template-columns: 1.1fr 1.4fr 1fr auto; cursor: pointer; }
.row-item:hover { border-color: #ccc7ba; }
.row-item h3 { font-size: 13.5px; font-weight: 500; }
.row-item .sub { font-size: 11.5px; color: var(--mute); }
.thumb { width: 44px; height: 54px; object-fit: cover; background: var(--paper-2); }
.status {
  display: inline-block; font-size: 10px; letter-spacing: .1em; text-transform: uppercase;
  padding: 3px 8px; border: 1px solid var(--line); color: var(--ink-soft); background: var(--paper-2);
}
.status.new { color: var(--bad); border-color: #e2c4bc; background: #fdf2ef; }
.status.paid { color: var(--ok); border-color: #c4d8c9; background: #f0f6f1; }
.status.shipped { color: #2f5a7a; border-color: #c3d5e2; background: #eff5f9; }
.status.done { color: var(--mute); }
.status.cancelled { color: var(--mute); text-decoration: line-through; }
.status.open { color: var(--bad); border-color: #e2c4bc; background: #fdf2ef; }
.status.closed { color: var(--mute); }

/* chat workspace */
.chat-workspace { display: grid; grid-template-columns: 300px 1fr; gap: 12px; height: calc(100vh - 130px); }
.conv-list { background: var(--card); border: 1px solid var(--line); overflow-y: auto; }
.conv-item { padding: 13px 15px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; gap: 3px; }
.conv-item:hover { background: var(--paper-2); }
.conv-item.active { background: var(--paper-2); border-left: 2px solid var(--accent); }
.conv-item b { font-size: 13px; font-weight: 500; }
.conv-item span { font-size: 11.5px; color: var(--mute); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-pane { background: var(--card); border: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.conv-empty { margin: auto; color: var(--mute); font-size: 13px; }
.conv-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; }
.conv-head b { font-size: 14px; }
.conv-head .sub { font-size: 11.5px; color: var(--mute); }
.conv-head .spacer { margin-left: auto; display: flex; gap: 8px; }
.conv-log { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 10px; background: var(--paper); }
.bubble { max-width: 72%; padding: 9px 13px; font-size: 13.5px; border-radius: 10px; white-space: pre-wrap; word-break: break-word; }
.bubble.visitor { background: #fff; border: 1px solid var(--line); align-self: flex-start; border-bottom-left-radius: 3px; }
.bubble.agent { background: var(--ink); color: #fff; align-self: flex-end; border-bottom-right-radius: 3px; }
.bubble time { display: block; font-size: 10px; opacity: .55; margin-top: 4px; }
.conv-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.conv-form input { min-height: 40px; }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20,18,14,.45); z-index: 100; display: grid; place-items: center; padding: 20px; overflow-y: auto; }
.modal { background: var(--paper); width: min(680px, 100%); max-height: 90vh; overflow-y: auto; padding: 26px; }
.modal h2 { font-family: var(--serif); font-size: 24px; margin-bottom: 4px; }
.modal .sub { font-size: 12px; color: var(--mute); margin-bottom: 20px; }
.modal-close { float: right; background: none; border: 0; font-size: 20px; line-height: 1; }
.form-grid { display: grid; gap: 12px; }
.form-grid.two { grid-template-columns: 1fr 1fr; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.detail-table { width: 100%; border-collapse: collapse; font-size: 13px; margin: 10px 0 18px; }
.detail-table th, .detail-table td { text-align: left; padding: 8px 0; border-bottom: 1px solid var(--line); }
.detail-table th { font-weight: 500; color: var(--mute); font-size: 11px; letter-spacing: .08em; text-transform: uppercase; }
.detail-table td.num { text-align: right; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translate(-50%, 14px);
  background: var(--ink); color: #fff; padding: 11px 18px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: .22s; z-index: 200; max-width: calc(100vw - 40px);
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1100px) {
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .two-col { grid-template-columns: 1fr; }
  .row-item.orders, .row-item.products { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .rail { position: static; height: auto; flex-direction: row; align-items: center; padding: 12px 16px; overflow-x: auto; gap: 8px; }
  .rail-brand { padding: 0 10px 0 0; font-size: 20px; }
  .rail-brand small { display: none; }
  .rail-nav { flex-direction: row; }
  .rail-nav button { padding: 8px 10px; white-space: nowrap; }
  .rail-foot { margin: 0 0 0 auto; padding: 0; flex-direction: row; align-items: center; }
  .rail-foot a, .rail-foot #railUser { display: none; }
  .chat-workspace { grid-template-columns: 1fr; height: auto; }
  .conv-list { max-height: 220px; }
  .conv-pane { height: 60vh; }
  .form-grid.two { grid-template-columns: 1fr; }
}
