:root {
  color-scheme: light dark;
  --app-height: 100dvh;
  --bg: #0c1016;
  --bg-2: #141b23;
  --surface: rgba(20, 26, 34, 0.82);
  --surface-strong: rgba(29, 37, 48, 0.92);
  --surface-soft: rgba(255, 255, 255, 0.06);
  --surface-tint: rgba(24, 116, 86, 0.11);
  --text: #f4f7fb;
  --muted: #a8b2c0;
  --line: rgba(255, 255, 255, 0.11);
  --line-strong: rgba(255, 255, 255, 0.22);
  --accent: #d72e40;
  --accent-2: #35d3b1;
  --safe: #31c48d;
  --cyan: #59c7f8;
  --amber: #f2bb57;
  --danger: #ff6b5f;
  --button: var(--tg-theme-button-color, #31c48d);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
  --radius: 8px;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: var(--app-height);
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background:
    linear-gradient(rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.028) 1px, transparent 1px),
    linear-gradient(145deg, #090c11 0%, #111822 48%, #0b1216 100%);
  background-size: 28px 28px, 28px 28px, auto;
  color: var(--text);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid rgba(183, 36, 50, 0.28);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-rows: auto auto auto auto auto auto auto minmax(0, 1fr);
  gap: 8px;
  width: min(1440px, 100%);
  height: var(--app-height);
  margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) 14px calc(16px + env(safe-area-inset-bottom));
  overflow: hidden;
}

.topbar,
.topbar-actions,
.main-tabs,
.section-head,
.pane-head,
.row-top,
.row-meta,
.detail-meta,
.action-dock,
.history-row,
.bulk-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar,
.section-head,
.pane-head {
  justify-content: space-between;
}

.topbar {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.035));
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

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

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 28px;
  line-height: 1.05;
  font-weight: 900;
}

h2 {
  margin-bottom: 0;
  font-size: 18px;
  line-height: 1.15;
}

h3 {
  margin-bottom: 8px;
  font-size: 15px;
  line-height: 1.2;
}

.status-pill,
.tag,
.state-tag {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.07);
  white-space: nowrap;
}

.status-pill[data-state="running"] {
  color: var(--safe);
  border-color: rgba(24, 116, 86, 0.35);
  background: rgba(24, 116, 86, 0.08);
}

.status-pill[data-state="paused"],
.status-pill[data-state="locked"] {
  color: var(--amber);
  border-color: rgba(154, 106, 19, 0.34);
  background: rgba(154, 106, 19, 0.08);
}

.status-pill[data-state="error"],
.state-tag.danger {
  color: var(--danger);
  border-color: rgba(179, 58, 50, 0.35);
  background: rgba(179, 58, 50, 0.08);
}

.state-tag.warn {
  color: var(--amber);
  border-color: rgba(154, 106, 19, 0.35);
  background: rgba(154, 106, 19, 0.08);
}

.icon-button,
.ghost-button,
.main-tab,
.folder-button,
.load-more,
.control-button,
.action-button,
.bulk-bar button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  min-width: 44px;
  padding: 0;
  font-weight: 800;
}

.ghost-button,
.main-tab,
.load-more,
.bulk-bar button {
  padding: 8px 12px;
}

.icon-button:disabled,
.ghost-button:disabled,
.main-tab:disabled,
.folder-button:disabled,
.load-more:disabled,
.control-button:disabled,
.action-button:disabled,
.bulk-bar button:disabled,
input:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.icon-button:hover:not(:disabled),
.ghost-button:hover:not(:disabled),
.main-tab:hover:not(:disabled),
.folder-button:hover,
.message-row:hover,
.control-button:hover:not(:disabled),
.action-button:hover:not(:disabled),
.bulk-bar button:hover:not(:disabled) {
  border-color: var(--line-strong);
  background: var(--surface-tint);
}

.notice {
  display: grid;
  gap: 4px;
  border: 1px solid rgba(183, 36, 50, 0.28);
  border-radius: var(--radius);
  padding: 12px;
  background: rgba(183, 36, 50, 0.07);
  color: var(--text);
}

.hidden {
  display: none !important;
}

.main-tabs {
  overflow-x: auto;
  scrollbar-width: thin;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 5px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(14px);
}

.main-tab {
  border-color: transparent;
  flex: 0 0 auto;
  min-width: 92px;
  background: transparent;
}

.main-tab.active,
.select-toggle.active {
  border-color: rgba(183, 36, 50, 0.42);
  color: #ffffff;
  background: var(--accent);
}

.ops-strip,
.dashboard,
.control-grid {
  display: grid;
  gap: 8px;
}

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

.ops-cell,
.metric {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.ops-cell {
  min-height: 56px;
  padding: 9px 12px;
}

.ops-cell span,
.metric span,
label,
.date {
  color: var(--muted);
  font-size: 12px;
}

.ops-cell span,
.metric span,
label {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ops-cell strong {
  display: block;
  margin-top: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 78px 184px 98px;
  gap: 8px;
  align-items: end;
}

.search-wrap,
.sort-wrap {
  display: grid;
  gap: 5px;
}

input,
select {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 12px;
  color: var(--text);
  background: var(--surface);
}

.toolbar-button,
.select-toggle {
  width: 100%;
}

.folder-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 1px 0 3px;
  scrollbar-width: thin;
}

.folder-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  padding: 8px 10px 8px 12px;
  white-space: nowrap;
}

.folder-button strong {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  min-height: 24px;
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-soft);
  font-size: 12px;
}

.folder-button.active {
  border-color: rgba(183, 36, 50, 0.42);
  color: #ffffff;
  background: var(--accent);
}

.folder-button.active strong {
  color: var(--accent);
  background: #ffffff;
}

.stage {
  display: grid;
  grid-template-columns: minmax(330px, 0.82fr) minmax(0, 1.18fr);
  gap: 10px;
  min-height: 0;
  overflow: hidden;
}

.inbox-pane,
.detail-pane,
.memory-pane,
.status-pane {
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.inbox-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.pane-head,
.section-head {
  min-height: 64px;
  border-bottom: 1px solid var(--line);
  padding: 12px;
}

.message-list,
.detail-pane,
.memory-pane,
.status-pane {
  overflow: auto;
  overscroll-behavior: contain;
}

.message-row {
  display: block;
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 12px;
  text-align: left;
  color: var(--text);
  background: transparent;
  cursor: pointer;
}

.message-row.active {
  box-shadow: inset 4px 0 0 var(--accent);
  background: var(--surface-tint);
}

.message-row.selected {
  background: rgba(24, 116, 86, 0.08);
}

.message-row:last-child {
  border-bottom: 0;
}

.row-top {
  justify-content: space-between;
}

.row-meta,
.detail-meta,
.action-dock,
.history-row {
  flex-wrap: wrap;
}

.subject {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  margin: 7px 0 5px;
  font-weight: 800;
  line-height: 1.25;
}

.subject span:last-child,
.summary {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
}

.subject span:last-child {
  -webkit-line-clamp: 2;
}

.summary {
  margin-bottom: 8px;
  color: var(--muted);
  line-height: 1.42;
  -webkit-line-clamp: 2;
}

.muted,
.detail-pane p {
  color: var(--muted);
  line-height: 1.45;
}

.sender {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--muted);
}

.date {
  display: block;
  flex: 0 0 auto;
}

.select-dot {
  display: none;
  width: 20px;
  height: 20px;
  border: 2px solid var(--line-strong);
  border-radius: 50%;
}

.message-row.selecting .select-dot {
  display: inline-block;
}

.message-row.selected .select-dot {
  border-color: var(--safe);
  background: radial-gradient(circle, var(--safe) 0 45%, transparent 48%);
}

.urgency-high,
.urgency-critical,
.urgency-urgent {
  color: var(--danger);
  border-color: rgba(179, 58, 50, 0.35);
  background: rgba(179, 58, 50, 0.08);
}

.urgency-normal,
.urgency-medium {
  color: var(--amber);
  border-color: rgba(154, 106, 19, 0.35);
  background: rgba(154, 106, 19, 0.08);
}

.load-more {
  width: calc(100% - 16px);
  margin: 8px;
}

.detail-pane {
  padding: 14px;
}

.detail-header h2 {
  margin-top: 10px;
  line-height: 1.2;
}

.detail-back {
  display: none;
  margin-bottom: 10px;
}

.action-dock {
  position: sticky;
  top: 0;
  z-index: 3;
  margin: 14px 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
}

.action-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  color: var(--muted);
  background: var(--surface-soft);
}

.action-button:hover:not(:disabled) {
  border-color: rgba(24, 116, 86, 0.34);
  background: rgba(24, 116, 86, 0.08);
}

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.08fr);
  gap: 10px;
}

.action-feedback {
  margin: -4px 0 12px;
  border: 1px solid rgba(24, 116, 86, 0.26);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--safe);
  background: rgba(24, 116, 86, 0.07);
}

.detail-section {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface);
}

.detail-section.priority {
  border-color: rgba(183, 36, 50, 0.24);
  background: rgba(183, 36, 50, 0.04);
}

.compact-section {
  background: var(--surface-tint);
}

.quote,
.draft-box {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface-soft);
  white-space: pre-wrap;
  line-height: 1.48;
}

.draft-box {
  max-height: 320px;
  overflow: auto;
}

.quote {
  max-height: 360px;
  overflow: auto;
}

.detail-list {
  margin: 0;
  padding-left: 18px;
  color: var(--muted);
  line-height: 1.45;
}

.history-row {
  justify-content: space-between;
  border-top: 1px solid var(--line);
  padding: 8px 0;
}

.history-row:first-child {
  border-top: 0;
  padding-top: 0;
}

.empty-state,
.message-empty {
  display: grid;
  place-items: center;
  text-align: center;
}

.empty-state {
  min-height: 420px;
}

.message-empty {
  min-height: 220px;
  gap: 8px;
  color: var(--muted);
}

.empty-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--muted);
  font-size: 18px;
}

.empty-mark.small {
  width: 38px;
  height: 38px;
  font-size: 16px;
}

.visual-icon {
  display: inline-grid;
  flex: 0 0 auto;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  color: var(--safe);
  background: rgba(24, 116, 86, 0.1);
  font-size: 12px;
  font-weight: 800;
}

.visual-icon::before {
  content: "✉";
}

.visual-icon.mini {
  width: 15px;
  height: 15px;
  border-radius: 4px;
  font-size: 9px;
}

.visual-icon.large {
  width: 58px;
  height: 58px;
  border-radius: var(--radius);
  font-size: 22px;
}

.visual-icon.refresh::before {
  content: "↻";
}

.visual-icon.fullscreen::before {
  content: "⛶";
}

.visual-icon.inbox::before {
  content: "✉";
}

.visual-icon.warning::before {
  content: "!";
}

.visual-icon.draft::before {
  content: "✎";
}

.visual-icon.clarify::before {
  content: "?";
}

.visual-icon.eye::before {
  content: "✓";
}

.visual-icon.unread::before {
  content: "●";
}

.visual-icon.queue::before {
  content: "…";
}

.visual-icon.clock::before {
  content: "◷";
}

.visual-icon.safe::before {
  content: "✓";
}

.visual-icon.brain::before {
  content: "☆";
}

.visual-icon.nosend::before {
  content: "×";
}

.visual-icon.pause::before {
  content: "Ⅱ";
}

.visual-icon.play::before {
  content: "▶";
}

.memory-pane,
.status-pane {
  display: none;
  padding: 0 0 14px;
}

body[data-view="memory"] .inbox-pane,
body[data-view="memory"] .detail-pane,
body[data-view="memory"] .status-pane,
body[data-view="status"] .inbox-pane,
body[data-view="status"] .detail-pane,
body[data-view="status"] .memory-pane {
  display: none;
}

body[data-view="memory"] .memory-pane,
body[data-view="status"] .status-pane {
  display: block;
}

body[data-view="memory"] .stage,
body[data-view="status"] .stage {
  grid-template-columns: 1fr;
}

.rules-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding: 12px;
}

.rule-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface-tint);
}

.rule-card p {
  margin-bottom: 0;
}

.muted-card {
  color: var(--muted);
}

.dashboard {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  padding: 12px;
}

.metric {
  min-height: 82px;
  padding: 12px;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 30px;
  line-height: 1;
}

.control-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  padding: 0 12px 12px;
}

.control-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  background: var(--surface-soft);
}

.bulk-bar {
  position: fixed;
  left: 50%;
  bottom: calc(10px + env(safe-area-inset-bottom));
  z-index: 20;
  transform: translateX(-50%);
  width: min(920px, calc(100% - 24px));
  justify-content: center;
  flex-wrap: wrap;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 8px;
  color: var(--text);
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  box-shadow: 0 12px 30px rgba(21, 24, 29, 0.14);
  backdrop-filter: blur(14px);
}

.bulk-bar strong {
  min-width: 92px;
}

.bulk-bar button {
  background: var(--surface-soft);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(74px + env(safe-area-inset-bottom));
  z-index: 30;
  transform: translateX(-50%);
  width: min(560px, calc(100% - 28px));
  border: 1px solid rgba(24, 116, 86, 0.28);
  border-radius: var(--radius);
  padding: 12px 14px;
  color: var(--safe);
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  box-shadow: 0 10px 28px rgba(21, 24, 29, 0.12);
}

.error-text {
  color: var(--danger);
}

@media (max-width: 980px) {
  .toolbar {
    grid-template-columns: minmax(0, 1fr) 78px 96px;
  }

  .sort-wrap {
    grid-column: 1 / -1;
  }

  .stage,
  .detail-grid,
  .rules-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .app-shell {
    grid-template-rows: auto auto auto auto minmax(0, 1fr);
    gap: 7px;
    padding: calc(10px + env(safe-area-inset-top)) 10px calc(12px + env(safe-area-inset-bottom));
  }

  h1 {
    font-size: 23px;
  }

  .topbar {
    min-height: 44px;
  }

  .brand-block .eyebrow,
  .ops-strip {
    display: none;
  }

  .toolbar {
    grid-template-columns: minmax(0, 1fr) 72px 88px;
    gap: 6px;
  }

  .toolbar .sort-wrap {
    display: none;
  }

  .main-tab {
    min-width: 86px;
  }

  .folder-tabs {
    padding-bottom: 1px;
  }

  .folder-button {
    min-height: 38px;
    padding: 6px 9px 6px 11px;
  }

  .stage {
    min-height: 0;
  }

  body[data-view="mail"]:not(.detail-open) .detail-pane {
    display: none;
  }

  body[data-view="mail"].detail-open .inbox-pane,
  body[data-view="mail"].detail-open .folder-tabs,
  body[data-view="mail"].detail-open .toolbar {
    display: none;
  }

  .detail-pane {
    padding: 12px;
  }

  .detail-back {
    display: inline-flex;
  }

  .action-dock {
    top: auto;
    bottom: 0;
    margin-bottom: 10px;
  }

  .action-button {
    flex: 1 1 calc(50% - 8px);
    justify-content: center;
  }

  .dashboard,
  .control-grid {
    grid-template-columns: 1fr 1fr;
  }

  .bulk-bar {
    justify-content: flex-start;
    overflow-x: auto;
    flex-wrap: nowrap;
  }

  .bulk-bar button,
  .bulk-bar strong {
    flex: 0 0 auto;
  }
}

@media (max-width: 420px) {
  .app-shell {
    padding-left: 8px;
    padding-right: 8px;
  }

  .topbar-actions {
    gap: 5px;
  }

  .status-pill {
    max-width: 86px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .metric strong {
    font-size: 27px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: var(--tg-theme-secondary-bg-color, #1b1d21);
    --surface-soft: #24272d;
    --surface-tint: #202923;
    --line: rgba(255, 255, 255, 0.13);
    --line-strong: rgba(255, 255, 255, 0.2);
  }

  .status-pill,
  .tag,
  .state-tag {
    background: rgba(255, 255, 255, 0.05);
  }
}
