:root {
  --bg: #f6f5f4;
  --paper: #fffdfa;
  --ink: #31302e;
  --muted: #615d59;
  --faint: #a39e98;
  --line: rgba(49, 48, 46, 0.12);
  --rail: #2b2926;
  --rail-ink: #d9d3c9;
  --olive: #6b7447;
  --olive-dark: #565f38;
  --wood: #b08457;
  --stone: #e7dfd1;
  --bad: #a4452f;
  --ok: #4f6b3a;
  --shadow: 0 1px 2px rgba(49, 48, 46, 0.04), 0 4px 18px rgba(49, 48, 46, 0.05);
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --font: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 400 15px/1.5 var(--font); -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; letter-spacing: -0.02em; font-weight: 600; }
h1 { font-size: 22px; letter-spacing: -0.03em; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
h4 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 22px 0 8px; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--olive); outline-offset: 2px; }

/* layout */
.app { display: flex; height: 100%; }
.rail {
  width: 84px; flex: none; background: var(--rail); color: var(--rail-ink);
  display: flex; flex-direction: column; align-items: center; padding: 16px 8px; gap: 4px;
}
.rail-brand {
  width: 40px; height: 40px; border-radius: 12px; background: var(--olive); color: #f3ecdc;
  display: grid; place-items: center; font-weight: 600; font-size: 18px; margin-bottom: 14px;
}
.rail-btn {
  width: 100%; background: none; border: 0; border-radius: 12px; color: inherit; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 4px; font-size: 11.5px;
  transition: background 0.25s var(--ease), color 0.25s var(--ease);
}
.rail-btn:hover { background: rgba(255, 255, 255, 0.07); }
.rail-btn[aria-current="page"] { background: rgba(255, 255, 255, 0.12); color: #fff; }
.rail-spacer { flex: 1; }
.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 18px 28px 14px; border-bottom: 1px solid var(--line); background: var(--paper);
}
.sub { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.badge {
  font-size: 12.5px; font-weight: 500; color: var(--olive-dark); background: #eef0e1;
  border: 1px solid rgba(107, 116, 71, 0.28); padding: 5px 12px; border-radius: 999px; white-space: nowrap;
}
.view { flex: 1; min-height: 0; }
.view[hidden] { display: none; }
.office-view { display: flex; }
.page { overflow: auto; padding: 24px 28px 40px; max-width: 880px; width: 100%; margin: 0 auto; }

/* stage */
.stage {
  position: relative; flex: 1; min-width: 0; background: #e9dfcd; overflow: hidden; touch-action: none;
}
#scene { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
.stage-tools { position: absolute; left: 16px; bottom: 44px; display: flex; gap: 6px; }
.tool {
  min-width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255, 253, 250, 0.92);
  cursor: pointer; font-size: 18px; box-shadow: var(--shadow); transition: transform 0.2s var(--ease), background 0.2s;
}
.tool.wide { font-size: 13px; padding: 0 14px; }
.tool:hover { background: #fff; transform: translateY(-1px); }
.stage-hint {
  position: absolute; left: 16px; bottom: 12px; margin: 0; font-size: 12.5px; color: var(--muted);
  background: rgba(255, 253, 250, 0.85); padding: 3px 10px; border-radius: 8px; max-width: calc(100% - 32px);
}

/* scene elements */
.has-bg .art { display: none; }
/* real background: static picture, only hover/selection feedback (no staged motion) */
.has-bg .desk { animation: none; }
.has-bg .desk .plate-name { font-size: 19px; }
.has-bg .desk .plate-duty { font-size: 13.5px; }
.has-bg .desk .ring { stroke-width: 4; }
.has-bg .desk:hover .ring, .has-bg .desk:focus-visible .ring { opacity: 0.6; }
.desk { cursor: pointer; outline: none; animation: rise 0.7s var(--ease) both; }
.desk .hit { fill: transparent; }
.desk .ring { fill: rgba(107, 116, 71, 0.08); stroke: var(--olive); stroke-width: 3; stroke-dasharray: 10 7; opacity: 0; transition: opacity 0.3s var(--ease); }
.desk .desk-art { transition: transform 0.35s var(--ease); }
.desk:hover .desk-art, .desk:focus-visible .desk-art { transform: translateY(-4px); }
.desk:hover .ring, .desk:focus-visible .ring { opacity: 0.55; }
.desk.selected .ring { opacity: 1; fill: rgba(107, 116, 71, 0.12); }
.desk .plate rect { fill: rgba(33, 31, 28, 0.9); stroke: rgba(246, 239, 224, 0.25); transition: fill 0.3s; }
.desk .plate-name { fill: #fffaf0; font: 700 14px var(--font); pointer-events: none; }
.desk .plate-duty { fill: #e9dfc9; font: 400 11.5px var(--font); pointer-events: none; }
.desk:hover .plate rect, .desk.selected .plate rect { fill: var(--olive-dark); }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* inspector */
.inspector {
  width: 400px; flex: none; background: var(--paper); border-left: 1px solid var(--line);
  overflow-y: auto; padding: 22px 22px 40px;
}
.insp-head { margin-bottom: 14px; animation: fade 0.35s var(--ease); }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.link { background: none; border: 0; color: var(--olive-dark); padding: 0 0 8px; cursor: pointer; font-size: 13px; }
.link:hover { text-decoration: underline; }
.agent-state { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 8px; vertical-align: middle; }
.agent-state .dot { margin: 0; }
.dot.off { background: var(--faint); }
.role-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.role-item {
  width: 100%; display: flex; align-items: center; gap: 12px; text-align: left; padding: 10px 12px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 12px; background: var(--paper); transition: background 0.2s, transform 0.2s var(--ease);
}
.role-item:hover { background: #faf6ee; transform: translateX(2px); }
.role-num { width: 28px; height: 28px; border-radius: 8px; background: var(--stone); display: grid; place-items: center; font-size: 13px; font-weight: 600; flex: none; }
.role-text { display: flex; flex-direction: column; line-height: 1.3; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin: 14px -4px 16px; overflow-x: auto; }
.tab {
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 9px 10px; cursor: pointer; color: var(--muted);
  font-weight: 500; white-space: nowrap; transition: color 0.2s, border-color 0.2s;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--olive); }
.tab-panel { animation: fade 0.3s var(--ease); }

/* forms */
.form { display: grid; gap: 8px; margin: 10px 0; }
.form label { font-size: 13px; font-weight: 500; color: var(--muted); }
input[type="text"], input[type="password"], textarea, select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; background: #fff; transition: border-color 0.2s, box-shadow 0.2s;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--olive); box-shadow: 0 0 0 3px rgba(107, 116, 71, 0.18); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 4px; }
.btn {
  border: 1px solid var(--line); background: var(--paper); border-radius: 10px; padding: 9px 16px; cursor: pointer; font-weight: 500;
  transition: background 0.2s, transform 0.15s var(--ease);
}
.btn:hover:not(:disabled) { background: #f3eee4; transform: translateY(-1px); }
.btn.primary { background: var(--olive); border-color: var(--olive); color: #fbf7ec; }
.btn.primary:hover:not(:disabled) { background: var(--olive-dark); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.form-msg { min-height: 1.3em; font-size: 13.5px; margin: 2px 0 0; }
.form-msg.ok { color: var(--ok); }
.form-msg.bad, .form-error { color: var(--bad); }

/* lists & cards */
.card, .notice, .empty {
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; box-shadow: var(--shadow);
}
.notice { background: #f3efe3; margin-bottom: 16px; box-shadow: none; font-size: 14px; }
.card-list { display: grid; gap: 10px; margin-bottom: 16px; }
.card-list.compact .card { padding: 12px 14px; box-shadow: none; }
.card summary { cursor: pointer; list-style: none; }
.card summary::-webkit-details-marker { display: none; }
.card[open] summary { margin-bottom: 10px; }
.job-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.job-body { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 6px; }
.chip { font-size: 12px; font-weight: 500; padding: 2px 9px; border-radius: 999px; background: var(--stone); color: var(--ink); white-space: nowrap; }
.chip.queued_manual { background: #eef0e1; color: var(--olive-dark); }
.chip.off, .chip.planned { background: #efece6; color: var(--muted); }
.skill-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.skill-list li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; }
.skill-list p { margin: 0; }
.history { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.history li { border-left: 2px solid var(--stone); padding-left: 10px; overflow-wrap: anywhere; }
.history p { margin: 2px 0; font-size: 14px; white-space: pre-wrap; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 10px 20px; margin: 12px 0 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; }
.empty { text-align: center; padding: 48px 24px; max-width: 560px; margin: 40px auto; }
.empty-art {
  width: 92px; height: 64px; margin: 0 auto 18px; border-radius: 16px; background: var(--stone); position: relative;
}
.empty-art::after { content: ""; position: absolute; left: 36px; top: 20px; border-left: 20px solid var(--olive); border-top: 12px solid transparent; border-bottom: 12px solid transparent; }
.empty h2 { margin-bottom: 8px; }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--rail); color: #f6efe0;
  padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none; transition: opacity 0.3s, transform 0.3s var(--ease); z-index: 20;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* auth pages */
.auth-body { display: grid; place-items: center; min-height: 100%; background: var(--bg); padding: 20px; }
.auth-card { width: 100%; max-width: 400px; background: var(--paper); border: 1px solid var(--line); border-radius: 18px; padding: 32px 28px; box-shadow: var(--shadow); }
.auth-card .badge { display: inline-block; margin: 8px 0 18px; }
.auth-mark { width: 44px; height: 44px; border-radius: 12px; background: var(--olive); margin-bottom: 16px; }
.auth-form { display: grid; gap: 12px; }
.auth-form label { display: grid; gap: 6px; font-size: 13.5px; font-weight: 500; color: var(--muted); }

/* header actions, personal page, dialogs */
.top-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.role-select { display: none; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.agent-view { overflow: auto; }
.agent-page { max-width: 880px; margin: 0 auto; padding: 20px 28px 48px; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.agent-page textarea, .agent-page input[type=text], .agent-page input[type=url], .agent-page input[type=number],
.agent-page input[type=search], .agent-page select, .modal input, .modal select { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.row input[type=search] { flex: 1; min-width: 160px; }
.check { display: flex; gap: 6px; align-items: center; font-size: 14px; }
.journey { list-style: none; margin: 14px 0; padding: 0; display: grid; gap: 6px; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); }
.step-btn { width: 100%; height: 100%; text-align: left; display: grid; gap: 4px; justify-items: start; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--paper); cursor: pointer; }
.step-btn:hover { background: #faf6ee; }
.step-n { width: 22px; height: 22px; border-radius: 6px; background: var(--stone); display: grid; place-items: center; font-size: 12px; font-weight: 600; }
.agent-page .btn { text-decoration: none; display: inline-block; }
.modal { position: fixed; inset: 0; background: rgba(33, 31, 28, 0.5); display: grid; place-items: center; z-index: 30; padding: 16px; overflow: auto; }
.modal[hidden] { display: none; }
.modal-card { background: var(--paper); border-radius: 16px; padding: 22px; width: 100%; max-width: 520px; max-height: 92vh; overflow: auto; }

/* phone */
@media (max-width: 720px) {
  .app { flex-direction: column-reverse; }
  .rail { width: 100%; flex-direction: row; padding: 4px 6px calc(4px + env(safe-area-inset-bottom)); justify-content: space-around; }
  .rail-brand, .rail-spacer { display: none; }
  .rail-btn { flex: 1; padding: 6px 2px; font-size: 10.5px; }
  .topbar { padding: 12px 16px 10px; }
  h1 { font-size: 19px; }
  .sub { display: none; }
  .office-view { flex-direction: column; }
  .stage { flex: none; height: 46vh; }
  .role-select { display: block; width: 100%; }
  .stage { flex: 1; }
  .page { padding: 16px 16px 24px; }
  .stage-hint { display: none; }
  .stage-tools { bottom: 12px; }
}

.advanced { margin-top: 1rem; }
.advanced > summary { cursor: pointer; font-weight: 600; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
  .desk:hover .desk-art, .desk:focus-visible .desk-art { transform: none; }
}
