:root {
  color-scheme: light;
  --touch-target: 44px;
  --tg-viewport-height: 100svh;
  --tg-safe-bottom: env(safe-area-inset-bottom);
  --bg: #f5f7f4;
  --panel: #ffffff;
  --panel-strong: #eef2ef;
  --surface: #fbfcfb;
  --text: #1d2320;
  --muted: #61706a;
  --line: #d7dfd9;
  --accent: #187c69;
  --accent-strong: #0e5e4f;
  --button-text: #ffffff;
  --blue: #27639b;
  --violet: #7050a4;
  --amber: #b06a1f;
  --danger: #b13b47;
  --shadow: 0 10px 28px rgba(24, 34, 29, 0.1);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

body,
button,
input,
textarea,
.object-row,
.chat-pane,
.latest-card {
  overflow-wrap: anywhere;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: var(--tg-viewport-height);
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr);
}

.app-shell.nav-collapsed {
  grid-template-columns: 78px minmax(0, 1fr);
}

.drawer-backdrop {
  display: none;
}

.nav-rail {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 12px;
  background: #e8ede8;
  border-right: 1px solid var(--line);
}

.nav-head,
.brand-lockup,
.topbar,
.chat-header,
.latest-head,
.pane-header,
.row-title-line,
.route-context {
  display: flex;
  align-items: center;
  gap: 12px;
}

.nav-head,
.topbar,
.chat-header,
.latest-head,
.pane-header,
.row-title-line {
  justify-content: space-between;
}

.brand-lockup {
  min-width: 0;
}

.brand-mark {
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 900;
}

.brand-copy {
  min-width: 0;
  display: grid;
  line-height: 1.12;
}

.brand-copy strong {
  font-size: 17px;
}

.brand-copy span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.icon-button,
.nav-item,
.source-chip,
.source-pill,
.text-button,
.command-button,
.send-button,
.mode-button,
.tab-button,
.object-row {
  min-height: var(--touch-target);
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--text);
}

.icon-button {
  flex: 0 0 auto;
  width: 48px;
  background: var(--panel);
  border-color: var(--line);
  font-weight: 900;
}

.mobile-menu {
  display: none;
}

.nav-group {
  display: grid;
  gap: 8px;
}

.nav-item {
  width: 100%;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  grid-template-areas:
    "icon text"
    "icon sub";
  column-gap: 10px;
  align-items: center;
  padding: 8px 10px;
  background: transparent;
  text-align: left;
}

.nav-icon {
  grid-area: icon;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.65);
  color: var(--accent-strong);
  font-weight: 900;
}

.nav-text {
  grid-area: text;
  min-width: 0;
  font-weight: 850;
}

.nav-item small {
  grid-area: sub;
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.nav-item.active {
  background: var(--panel);
  border-color: var(--line);
  box-shadow: var(--shadow);
}

.app-shell.nav-collapsed .brand-copy,
.app-shell.nav-collapsed .nav-item small,
.app-shell.nav-collapsed .nav-text,
.app-shell.nav-collapsed .nav-sources {
  display: none;
}

.app-shell.nav-collapsed .nav-item {
  grid-template-columns: 1fr;
  grid-template-areas: "icon";
  justify-items: center;
  padding: 8px;
}

.nav-sources,
.source-strip {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.source-chip,
.source-pill {
  min-width: 0;
  padding: 0 10px;
  background: rgba(255, 255, 255, 0.62);
  border-color: var(--line);
  font-size: 13px;
  font-weight: 850;
  line-height: 1.1;
}

.source-chip.active,
.source-pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--button-text);
}

.panel {
  min-width: 0;
  padding: 18px 20px 28px;
}

.topbar {
  min-width: 0;
}

.topbar-title {
  min-width: 0;
  flex: 1;
}

.topbar h1,
.chat-header h2,
.pane-header h2 {
  margin: 0;
  line-height: 1.15;
}

.topbar h1 {
  font-size: 28px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  text-transform: uppercase;
}

.search-band {
  margin: 16px 0 12px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.search-label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

#searchInput {
  width: 100%;
  min-height: 48px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

.mode-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin: 0 0 12px;
}

.mode-button {
  min-width: 0;
  padding: 0 10px;
  background: var(--panel);
  border-color: var(--line);
  font-weight: 850;
  line-height: 1.1;
}

.mode-button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--button-text);
}

.source-strip {
  display: none;
}

.workspace-grid {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(260px, 340px) minmax(220px, 300px) minmax(360px, 1fr);
  gap: 14px;
  align-items: start;
}

.list-pane,
.lane-pane,
.chat-pane {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.list-pane,
.lane-pane {
  padding: 12px;
}

.counter {
  flex: 0 0 auto;
  min-width: 34px;
  padding: 4px 8px;
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--muted);
  text-align: center;
  font-weight: 850;
}

.object-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.object-list.compact {
  gap: 7px;
}

.object-row {
  width: 100%;
  min-height: 92px;
  padding: 12px;
  background: var(--surface);
  border-color: var(--line);
  text-align: left;
}

.object-row.mini {
  min-height: 74px;
  background: var(--panel-strong);
}

.object-row.active {
  border-color: var(--accent);
  background: #edf8f4;
}

.row-title {
  min-width: 0;
  font-size: 17px;
  font-weight: 875;
  line-height: 1.2;
}

.row-path {
  margin-top: 5px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 780;
  line-height: 1.25;
}

.row-summary {
  margin: 8px 0 0;
  color: var(--muted);
  line-height: 1.35;
}

.badge,
.status-badge,
.context-chip {
  flex: 0 0 auto;
  padding: 4px 8px;
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--muted);
  font-size: 12px;
  font-weight: 850;
}

.badge.needs-reply,
.status-badge.needs-reply {
  background: #fff1dc;
  color: var(--amber);
}

.badge.blocked,
.status-badge.blocked,
.badge.expired,
.status-badge.expired {
  background: #ffe8eb;
  color: var(--danger);
}

.badge.active,
.status-badge.active,
.badge.open,
.status-badge.open {
  background: #e3f5ef;
  color: var(--accent-strong);
}

.badge.watching,
.status-badge.watching {
  background: #e5f1ff;
  color: var(--blue);
}

.badge.handoff-requested,
.status-badge.handoff-requested {
  background: #f1e9ff;
  color: var(--violet);
}

.chat-pane {
  padding: 14px;
}

.chat-header h2 {
  font-size: 25px;
}

.route-context {
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-top: 12px;
}

.context-chip.strong {
  background: #e3f5ef;
  color: var(--accent-strong);
}

.meta-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.meta-grid div {
  min-width: 0;
  padding: 10px;
  border-radius: 8px;
  background: var(--panel-strong);
}

.meta-grid span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 850;
  text-transform: uppercase;
}

.meta-grid strong {
  display: block;
  margin-top: 4px;
  overflow-wrap: anywhere;
  font-size: 14px;
}

.subchat-tabs {
  display: flex;
  gap: 8px;
  margin: 14px -2px 0;
  overflow-x: auto;
  padding: 2px;
  scrollbar-width: thin;
}

.tab-button {
  flex: 0 0 auto;
  min-height: var(--touch-target);
  padding: 0 12px;
  background: var(--surface);
  border-color: var(--line);
  font-weight: 850;
}

.tab-button.active {
  border-color: var(--accent);
  background: #e3f5ef;
  color: var(--accent-strong);
}

.latest-card {
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.latest-card h3 {
  margin: 0;
  font-size: 18px;
}

.latest-card p {
  margin: 10px 0 0;
  line-height: 1.45;
}

.text-button {
  padding: 0 10px;
  background: transparent;
  color: var(--accent-strong);
  font-weight: 850;
}

.command-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.command-button {
  min-width: 0;
  padding: 0 8px;
  background: var(--panel-strong);
  border-color: var(--line);
  font-weight: 850;
  line-height: 1.12;
  white-space: normal;
}

.command-button.primary,
.send-button {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--button-text);
}

.reply-box {
  display: grid;
  gap: 8px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.reply-box label {
  color: var(--muted);
  font-weight: 850;
}

.reply-box textarea {
  width: 100%;
  min-height: 96px;
  resize: vertical;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

.send-button {
  font-weight: 875;
}

.bottom-nav {
  display: none;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  z-index: 20;
  max-width: min(460px, calc(100vw - 32px));
  transform: translateX(-50%) translateY(20px);
  opacity: 0;
  padding: 10px 14px;
  border-radius: 8px;
  background: #202723;
  color: #ffffff;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 1100px) {
  .workspace-grid {
    grid-template-columns: minmax(270px, 340px) minmax(0, 1fr);
  }

  .lane-pane {
    order: 3;
    grid-column: 1 / -1;
  }

  .command-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 820px) {
  .app-shell,
  .app-shell.nav-collapsed {
    display: block;
    min-height: var(--tg-viewport-height);
    padding-bottom: calc(74px + var(--tg-safe-bottom));
  }

  .drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 14;
    display: block;
    pointer-events: none;
    opacity: 0;
    border: 0;
    background: rgba(20, 26, 23, 0.32);
    transition: opacity 160ms ease;
  }

  .nav-open .drawer-backdrop {
    pointer-events: auto;
    opacity: 1;
  }

  .nav-rail {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 15;
    width: min(336px, calc(100vw - 36px));
    transform: translateX(-104%);
    transition: transform 180ms ease;
    box-shadow: var(--shadow);
  }

  .nav-open .nav-rail {
    transform: translateX(0);
  }

  .app-shell.nav-collapsed .brand-copy,
  .app-shell.nav-collapsed .nav-item small,
  .app-shell.nav-collapsed .nav-text,
  .app-shell.nav-collapsed .nav-sources {
    display: grid;
  }

  .app-shell.nav-collapsed .nav-item {
    grid-template-columns: 34px minmax(0, 1fr);
    grid-template-areas:
      "icon text"
      "icon sub";
    justify-items: stretch;
    padding: 8px 10px;
  }

  .mobile-menu {
    display: block;
  }

  .panel {
    padding: 12px;
  }

  .topbar {
    align-items: flex-start;
  }

  .topbar h1 {
    font-size: 23px;
  }

  .source-strip {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin-bottom: 16px;
  }

  .mode-strip {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .workspace-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .chat-pane {
    order: 1;
  }

  .lane-pane {
    order: 2;
    grid-column: auto;
  }

  .list-pane {
    order: 3;
  }

  .chat-header {
    align-items: flex-start;
  }

  .chat-header h2 {
    font-size: 22px;
  }

  .meta-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .command-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    padding: 8px;
    margin-left: -8px;
    margin-right: -8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(8px);
  }

  .bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 12;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
    padding: 8px 6px calc(8px + var(--tg-safe-bottom));
    background: #e8ede8;
    border-top: 1px solid var(--line);
  }

  .bottom-nav .nav-item {
    min-height: 48px;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-areas: "text";
    padding: 0 4px;
    text-align: center;
    font-size: 12px;
    font-weight: 850;
  }

  .bottom-nav .nav-item .nav-icon,
  .bottom-nav .nav-item small {
    display: none;
  }
}

@media (max-width: 430px) {
  .panel {
    padding: 10px;
  }

  .icon-button {
    width: 46px;
  }

  .object-row {
    min-height: 98px;
  }

  .meta-grid {
    grid-template-columns: 1fr;
  }

  .source-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 1520px) {
  .workspace-grid {
    grid-template-columns: minmax(320px, 420px) minmax(300px, 380px) minmax(620px, 1fr);
  }

  .chat-pane {
    position: sticky;
    top: 18px;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #181b19;
    --panel: #222722;
    --panel-strong: #2d342f;
    --surface: #1c211e;
    --text: #eef2ed;
    --muted: #a7b0aa;
    --line: #3c453f;
    --accent: #40a88f;
    --accent-strong: #78d3bd;
    --blue: #8ebff0;
    --violet: #b6a0ea;
    --amber: #e1a65f;
    --danger: #f08a95;
    --shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
  }

  .nav-rail,
  .bottom-nav {
    background: #202720;
  }

  .brand-mark,
  .mode-button.active,
  .source-chip.active,
  .source-pill.active,
  .command-button.primary,
  .send-button {
    color: #ffffff;
  }

  .nav-icon {
    background: rgba(255, 255, 255, 0.08);
  }

  .object-row.active {
    background: #16342d;
    color: var(--text);
  }

  .object-row.active .row-summary {
    color: #c8d6cf;
  }

  .context-chip.strong,
  .tab-button.active,
  .badge.active,
  .status-badge.active,
  .badge.open,
  .status-badge.open {
    background: #173b32;
    color: var(--accent-strong);
  }

  .command-grid {
    background: rgba(34, 39, 34, 0.96);
  }
}
