:root {
  --bg: #eef1ec;
  --bg-top: #f4f5ef;
  --paper: #fffdf5;
  --sidebar: #f3f1e8;
  --ink: #141c1e;
  --muted: #5d6864;
  --line: rgba(20, 28, 30, 0.12);
  --line-strong: rgba(20, 28, 30, 0.22);
  --accent: #b4442d;
  --accent-strong: #7e2418;
  --gold: #c9a24a;
  --steel: #4f6367;
  --shadow: 0 18px 44px rgba(18, 32, 35, 0.1);
  --sans: "Segoe UI", "Aptos", system-ui, sans-serif;
  --serif: "Iowan Old Style", Palatino, "Palatino Linotype", Georgia, serif;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  --sidebar-w: 240px;
  --rail-w: 300px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--sans);
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(18, 32, 35, 0.045) 1px, transparent 1px),
    linear-gradient(180deg, rgba(18, 32, 35, 0.035) 1px, transparent 1px),
    linear-gradient(180deg, var(--bg-top), var(--bg));
  background-size: 56px 56px, 56px 56px, auto;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
a { color: var(--accent-strong); }

.sr { display: grid; gap: 0.4rem; font-size: 13px; color: var(--muted); }
.hidden, [hidden] { display: none !important; }

.lock {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}
.lock-card {
  width: min(420px, 100%);
  background: var(--paper);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  border-radius: 18px;
  padding: 2rem 1.7rem 1.6rem;
  display: grid;
  gap: 0.85rem;
}
.lock-kicker {
  margin: 0;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 11px;
  color: var(--accent);
}
.lock-card h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 42px;
  margin: 0;
  letter-spacing: -0.03em;
}
.lock-lead { margin: 0; color: var(--muted); line-height: 1.5; }
.lock-card input {
  width: 100%;
  border: 1px solid var(--line-strong);
  background: #f7f5ee;
  border-radius: 10px;
  padding: 0.75rem 0.9rem;
}
#pin { letter-spacing: 0.4em; font-size: 22px; text-align: center; }
.name-label { display: grid; gap: 0.35rem; font-size: 13px; color: var(--muted); }
.lock-card button[type="submit"] {
  border: 0;
  background: var(--ink);
  color: var(--paper);
  border-radius: 10px;
  padding: 0.8rem 1rem;
}
.lock-error { margin: 0; color: var(--accent); font-size: 13px; }

.app {
  min-height: 100vh;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr var(--rail-w);
}

.sidebar, .rail {
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  min-height: 100vh;
  padding: 1rem 0.9rem 1.4rem;
}
.rail { border-right: 0; border-left: 1px solid var(--line); display: flex; flex-direction: column; gap: 0.75rem; }

.brand {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.2rem 0.4rem 0.9rem;
}
.brand h1 {
  font-family: var(--serif);
  font-size: 26px;
  font-weight: 500;
  margin: 0;
  letter-spacing: -0.03em;
}
.brand span { font-size: 11px; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; }

.nav-label {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0.4rem 0.5rem;
}
.page-list { display: grid; gap: 0.2rem; }
.page-item {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 0.45rem 0.5rem;
  color: var(--ink);
}
.page-item:hover, .page-item.active { background: rgba(20, 28, 30, 0.06); }
.page-item .kind {
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  width: 42px;
}
.side-actions { display: grid; gap: 0.35rem; margin-top: 0.9rem; padding: 0 0.25rem; }
.side-actions button, .ghost, .tiny {
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  font-size: 13px;
}
.tiny { padding: 0.2rem 0.45rem; font-size: 12px; }

.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: transparent; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: rgba(251, 250, 244, 0.72);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.who { display: flex; align-items: center; gap: 0.5rem; font-size: 13px; color: var(--muted); }
.who input {
  width: 140px;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 8px;
  padding: 0.3rem 0.5rem;
}
.live-dot {
  width: 8px; height: 8px; border-radius: 99px; background: #3d8b5e; display: inline-block;
}
.live-dot.off { background: #b4442d; }
.top-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }

.main {
  padding: 1.25rem 1.5rem 4rem;
  min-height: calc(100vh - 58px);
}

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.1rem; }
.page-title {
  font-family: var(--serif);
  font-size: 40px;
  font-weight: 500;
  border: 0;
  background: transparent;
  width: min(720px, 100%);
  padding: 0;
  letter-spacing: -0.03em;
}
.page-meta { display: flex; gap: 0.4rem; flex-wrap: wrap; color: var(--muted); font-size: 12px; }

.blocks { display: grid; gap: 0.55rem; width: min(760px, 100%); }
.block {
  position: relative;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.85rem 0.95rem 0.9rem;
  box-shadow: 0 8px 18px rgba(18, 32, 35, 0.04);
}
.block:focus-within { border-color: var(--line-strong); }
.block-bar {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.45rem;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.block-bar span { display: flex; gap: 0.45rem; align-items: center; }
.drag { cursor: grab; color: var(--muted); border: 0; background: none; padding: 0 0.2rem; }
.heading-input, .text-box, .code-box, .html-source {
  width: 100%;
  border: 0;
  background: transparent;
  resize: vertical;
  outline: none;
}
.heading-input { font-family: var(--serif); font-size: 28px; font-weight: 500; }
.text-box { min-height: 88px; line-height: 1.55; }
.md { line-height: 1.6; }
.md h1, .md h2, .md h3 { font-family: var(--serif); font-weight: 500; margin: 0.2rem 0 0.4rem; }
.md p { margin: 0 0 0.5rem; }
.md code, .code-view, .code-box {
  font-family: var(--mono);
  font-size: 13px;
}
.code-box, .code-view, .html-source {
  background: #f4f1e8;
  border-radius: 10px;
  padding: 0.75rem;
  min-height: 96px;
  white-space: pre-wrap;
}
.sandbox {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  min-height: 160px;
}
.check-list { display: grid; gap: 0.35rem; }
.check-row { display: flex; gap: 0.45rem; align-items: center; }
.check-row input[type="text"] { flex: 1; border: 0; background: transparent; padding: 0.25rem 0; }
.check-row input[type="text"].done { text-decoration: line-through; color: var(--muted); }

.add-row { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.8rem; width: min(760px, 100%); }
.add-row button, .top-actions button, .composer button, .modal-card button {
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  font-size: 13px;
}
.add-row button:hover, .top-actions button:hover { border-color: var(--ink); }

.board {
  display: flex;
  gap: 0.8rem;
  overflow-x: auto;
  padding-bottom: 1rem;
  align-items: flex-start;
}
.column {
  flex: 1 1 240px;
  min-width: 240px;
  max-width: 340px;
  background: rgba(255, 253, 245, 0.7);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.7rem;
  min-height: 220px;
}
.column header { display: flex; justify-content: space-between; align-items: center; gap: 0.4rem; margin-bottom: 0.55rem; }
.column header input {
  border: 0; background: transparent; font-weight: 600; width: 100%;
}
.column.steel { box-shadow: inset 3px 0 0 var(--steel); }
.column.accent { box-shadow: inset 3px 0 0 var(--accent); }
.column.gold { box-shadow: inset 3px 0 0 var(--gold); }
.cards { display: grid; gap: 0.5rem; min-height: 40px; }
.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.7rem;
  display: grid;
  gap: 0.35rem;
}
.card h3 { margin: 0; font-size: 14px; }
.card p { margin: 0; font-size: 13px; color: var(--muted); white-space: pre-wrap; }
.card.dragging { opacity: 0.5; }

.rail h2 { font-size: 13px; letter-spacing: 0.12em; text-transform: uppercase; margin: 0.2rem 0.2rem 0; color: var(--muted); font-weight: 600; }
.presence { display: flex; flex-wrap: wrap; gap: 0.3rem; padding: 0 0.2rem; }
.chip {
  font-size: 11px;
  border-radius: 99px;
  padding: 0.15rem 0.5rem;
  background: var(--paper);
  border: 1px solid var(--line);
}
.chip.agent { border-color: var(--gold); }
.thread { flex: 1; overflow: auto; display: grid; gap: 0.55rem; align-content: start; padding: 0.2rem; max-height: calc(100vh - 210px); }
.msg {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem 0.65rem;
  font-size: 13px;
}
.msg .meta { font-size: 11px; color: var(--muted); margin-bottom: 0.2rem; }
.msg.agent { border-color: rgba(201, 162, 74, 0.5); }
.msg.system { color: var(--muted); background: transparent; }
.composer { display: grid; gap: 0.4rem; margin-top: auto; }
.composer textarea {
  width: 100%;
  min-height: 72px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  padding: 0.55rem 0.65rem;
  resize: vertical;
}

.modal {
  position: fixed; inset: 0; background: rgba(18, 32, 35, 0.35);
  display: grid; place-items: center; padding: 1rem; z-index: 20;
}
.modal-card {
  width: min(980px, 100%);
  max-height: calc(100vh - 2rem);
  overflow: auto;
  background: var(--paper);
  border-radius: 16px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 1rem;
  display: grid;
  gap: 0.7rem;
}
.modal-card h2 { margin: 0; font-family: var(--serif); font-weight: 500; }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 0.7rem; min-height: 360px; }
.split textarea { min-height: 360px; font-family: var(--mono); font-size: 13px; border: 1px solid var(--line); border-radius: 10px; padding: 0.7rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.4rem; }
.primary { background: var(--ink) !important; color: var(--paper) !important; border-color: var(--ink) !important; }

.mobile-bar { display: none; }
.mobile-only { display: none !important; }
.scrim, .dock, .more-sheet { display: none; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 5rem;
  transform: translateX(-50%) translateY(12px);
  background: var(--ink);
  color: var(--paper);
  padding: 0.55rem 0.9rem;
  border-radius: 99px;
  font-size: 13px;
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 960px) {
  html, body {
    height: 100%;
    overflow: hidden;
  }
  body {
    background-size: 48px 48px, 48px 48px, auto;
  }
  .lock {
    min-height: 100dvh;
    padding: max(1.25rem, env(safe-area-inset-top)) 1rem max(1.25rem, env(safe-area-inset-bottom));
    align-items: end;
  }
  @media (min-height: 640px) {
    .lock { align-items: center; }
  }
  .lock-card {
    padding: 1.4rem 1.2rem 1.2rem;
    border-radius: 16px;
  }
  .lock-card h1 { font-size: 34px; }
  .lock-card input, .lock-card button[type="submit"] {
    min-height: 48px;
    font-size: 16px;
  }
  #pin { font-size: 22px; }

  .app {
    display: block;
    height: 100dvh;
    overflow: hidden;
    padding-bottom: calc(56px + env(safe-area-inset-bottom));
  }
  .stage {
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }
  .topbar {
    padding: 0.55rem 0.85rem;
    padding-top: max(0.55rem, env(safe-area-inset-top));
    gap: 0.5rem;
  }
  .who { min-width: 0; flex: 1; }
  .top-page-name {
    font-family: var(--serif);
    font-size: 18px;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
  }
  .icon-btn, .dock button, .more-sheet button, .sheet-head button {
    min-height: 44px;
    min-width: 44px;
    padding: 0.45rem 0.75rem;
    font-size: 15px;
  }
  .top-actions { display: none; }
  .mobile-only { display: inline-flex !important; }

  .main {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 0.75rem 0.85rem 1.25rem;
  }
  .page-head {
    flex-direction: column;
    gap: 0.35rem;
    margin-bottom: 0.75rem;
  }
  .page-title {
    font-size: 26px;
    width: 100%;
    min-height: 44px;
  }
  .page-item {
    min-height: 48px;
    padding: 0.7rem 0.65rem;
    font-size: 16px;
  }
  .side-actions button { min-height: 44px; }
  .page-meta { display: none; }
  .blocks, .add-row { width: 100%; }
  .block { padding: 0.7rem 0.75rem; border-radius: 12px; }
  .block-bar { font-size: 10px; }
  .drag { display: none; }
  .heading-input { font-size: 22px; }
  .text-box, .code-box, .check-row input[type="text"],
  .column header input, [data-card-title], input[data-field] {
    font-size: 16px !important;
  }
  .text-box, .code-box {
    min-height: 72px;
    overflow: hidden;
    resize: none;
  }
  .check-row { min-height: 44px; }
  .check-row input[type="checkbox"] { width: 22px; height: 22px; }
  .add-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.35rem;
    margin-top: 0.65rem;
  }
  .add-row button {
    flex: 0 0 auto;
    min-height: 44px;
    padding: 0.45rem 0.85rem;
    font-size: 14px;
  }

  .board {
    display: flex;
    gap: 0.7rem;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    scroll-padding-inline: 0.85rem;
    -webkit-overflow-scrolling: touch;
    padding: 0 0 0.4rem;
    align-items: stretch;
    scrollbar-width: none;
  }
  .board::-webkit-scrollbar { display: none; }
  .column {
    flex: 0 0 min(86vw, 340px);
    min-width: min(86vw, 340px);
    max-width: none;
    scroll-snap-align: start;
    min-height: 50dvh;
  }
  .card input, .card textarea { font-size: 16px; }
  .card { padding: 0.75rem; }
  .card[draggable="true"] { -webkit-user-drag: none; }

  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    border: 0;
    padding: 0;
    background: rgba(18, 32, 35, 0.42);
    z-index: 14;
  }
  .scrim[hidden] { display: none !important; }

  .sidebar, .rail, .more-sheet {
    position: fixed;
    left: 0;
    right: 0;
    width: 100%;
    z-index: 16;
    background: var(--sidebar);
    box-shadow: var(--shadow);
    transition: transform 0.22s ease;
    padding: 0.65rem 0.9rem calc(0.9rem + env(safe-area-inset-bottom));
  }
  .sidebar, .more-sheet, .rail {
    border-right: 0;
    min-height: 0;
  }
  .sidebar {
    top: auto;
    bottom: calc(56px + env(safe-area-inset-bottom));
    max-height: min(78dvh, var(--kbd-vh, 78dvh));
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 16px 16px 0 0;
    transform: translateY(110%);
  }
  .rail {
    top: auto;
    bottom: calc(56px + env(safe-area-inset-bottom) + var(--kb, 0px));
    height: min(72dvh, var(--kbd-vh, 72dvh));
    max-height: min(72dvh, var(--kbd-vh, 72dvh));
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    border-radius: 16px 16px 0 0;
    transform: translateY(120%);
    padding-bottom: 0.75rem;
  }
  body.kbd .rail {
    bottom: var(--kb, 0px);
    height: min(100dvh, var(--kbd-vh, 100dvh));
    max-height: min(100dvh, var(--kbd-vh, 100dvh));
  }
  .sidebar.open, .rail.open { transform: translateY(0); }
  .thread {
    max-height: none;
    flex: 1;
    min-height: 0;
  }
  .composer {
    grid-template-columns: 1fr auto;
    align-items: end;
    gap: 0.45rem;
  }
  .composer textarea {
    min-height: 48px;
    font-size: 16px;
    margin: 0;
  }
  .composer button { min-height: 48px; padding: 0 1rem; }

  .more-sheet {
    display: grid;
    gap: 0.45rem;
    top: auto;
    bottom: calc(56px + env(safe-area-inset-bottom));
    border-radius: 16px 16px 0 0;
    transform: translateY(110%);
    z-index: 17;
  }
  .more-sheet:not([hidden]) { transform: translateY(0); }
  .more-sheet[hidden] { display: none !important; }
  .more-sheet button {
    width: 100%;
    text-align: left;
    border: 1px solid var(--line);
    background: var(--paper);
    border-radius: 10px;
  }
  .sheet-grab {
    width: 40px;
    height: 4px;
    border-radius: 99px;
    background: var(--line-strong);
    margin: 0.15rem auto 0.45rem;
  }
  .sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
  }
  .sheet-head h2 { margin: 0; }

  .dock {
    display: grid;
    grid-template-columns: 1fr 1.3fr 1fr;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 18;
    background: rgba(243, 241, 232, 0.96);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--line);
    padding: 0.3rem 0.4rem calc(0.3rem + env(safe-area-inset-bottom));
    gap: 0.25rem;
  }
  .dock button {
    border: 0;
    background: transparent;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
  }
  .dock button.active {
    background: var(--paper);
    color: var(--ink);
  }
  body.kbd .dock { display: none; }
  body.kbd .app { padding-bottom: 0; }

  .modal {
    padding: 0;
    align-items: stretch;
  }
  .modal-card {
    width: 100%;
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
    padding: 0.85rem 0.85rem calc(0.85rem + env(safe-area-inset-bottom));
  }
  .split {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
    min-height: 0;
    height: min(56dvh, var(--kbd-vh, 56dvh));
  }
  .split textarea { min-height: 0; font-size: 16px; }
  .toast { bottom: calc(5.5rem + env(safe-area-inset-bottom)); }
}
