:root {
  color-scheme: dark;
  --ink: #e9f0f7;
  --muted: #9fb0c2;
  --line: #28435f;
  --panel: #13263b;
  --panel-2: #0f2033;
  --soft: #0a1726;
  --accent: #18a999;
  --accent-2: #f28c38;
  --danger: #ff8b7b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: #071525;
  font-family: "Segoe UI", Arial, sans-serif;
}

.shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  min-height: 100vh;
}

.workspace {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

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

h1 {
  margin: 0;
  font-size: 30px;
  letter-spacing: 0;
}

.title-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.mode-badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 9px;
  color: var(--muted);
  background: #09182a;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.mode-badge.ai {
  border-color: #7a5ccf;
  color: #d9ccff;
  background: #1d1733;
}

.mode-badge.rules {
  border-color: #2d6570;
  color: #a6e3d7;
  background: #0b2732;
}

.status {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
}

.status.ok {
  color: var(--accent);
}

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

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logout-button {
  height: 36px;
  border: 1px solid var(--line);
  background: #09182a;
  color: var(--muted);
  padding: 0 12px;
}

.export-button {
  height: 36px;
  border: 1px solid #2d6570;
  background: #0b2732;
  color: #a6e3d7;
  padding: 0 12px;
  font-size: 14px;
}

.export-button:disabled {
  color: var(--muted);
  border-color: var(--line);
  background: #09182a;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  padding: 0 12px;
  text-decoration: none;
  font-size: 14px;
}

.hidden {
  display: none;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.kpi-grid article {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
  min-height: 88px;
  min-width: 0;
}

.kpi-grid span {
  display: block;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 13px;
}

.kpi-grid strong {
  display: block;
  font-size: 18px;
  line-height: 1.1;
  white-space: nowrap;
  word-break: normal;
  font-variant-numeric: tabular-nums;
}

.visual-area {
  display: grid;
  grid-template-rows: 390px minmax(260px, 1fr);
  gap: 16px;
}

.charts-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 420px);
  gap: 16px;
  height: 100%;
  min-height: 0;
}

.chart-wrap,
.table-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  overflow: hidden;
}

.chart-wrap {
  padding: 16px;
  min-height: 0;
}

.pie-wrap {
  min-width: 0;
}

canvas {
  width: 100%;
  height: 100%;
}

.table-wrap {
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #102238;
  color: #b7c5d6;
  font-size: 12px;
  text-transform: uppercase;
}

td {
  max-width: 360px;
}

.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  border-left: 1px solid var(--line);
  background: #0b1b2d;
  max-height: 100vh;
}

.chat-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  padding: 12px 14px;
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
}

.help-button {
  height: 32px;
  border: 1px solid #2d6570;
  background: #0b2732;
  color: #a6e3d7;
  padding: 0 10px;
  font-size: 12px;
}

.messages {
  overflow: auto;
  padding: 20px;
}

.message {
  margin-bottom: 12px;
  padding: 12px 14px;
  border-radius: 8px;
  line-height: 1.45;
  font-size: 14px;
}

.message p {
  margin: 0;
}

.message-source {
  display: block;
  margin-top: 7px;
  color: var(--muted);
  font-size: 11px;
}

.message.assistant {
  background: #123142;
  border: 1px solid #25506a;
}

.message.user {
  margin-left: 36px;
  color: #fff;
  background: #1a6f66;
}

.message.error {
  background: #3a1a1a;
  border: 1px solid #7d3430;
  color: var(--danger);
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.voice-button {
  border: 1px solid #2d6570;
  background: #0b2732;
  color: #a6e3d7;
  padding: 0 12px;
}

.voice-button.listening {
  border-color: var(--accent-2);
  background: #3a2412;
  color: #ffd5ad;
}

input,
button {
  height: 44px;
  border-radius: 8px;
  font: inherit;
}

input {
  min-width: 0;
  border: 1px solid var(--line);
  background: #09182a;
  color: var(--ink);
  padding: 0 12px;
}

button {
  border: 0;
  background: var(--accent);
  color: #fff;
  padding: 0 18px;
  font-weight: 700;
  cursor: pointer;
}

button:disabled {
  opacity: .55;
  cursor: wait;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(3, 10, 18, .72);
  padding: 18px;
}

.modal-backdrop.hidden {
  display: none;
}

.modal-panel {
  width: min(860px, 100%);
  max-height: min(760px, calc(100vh - 36px));
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(0, 0, 0, .35);
}

.modal-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  padding: 18px;
}

.modal-header h2 {
  margin: 0;
  font-size: 22px;
}

.modal-note {
  max-width: 620px;
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.icon-button {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  background: #09182a;
  color: var(--muted);
  padding: 0;
}

.question-groups {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  padding: 18px;
}

.question-groups section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  padding: 14px;
}

.question-groups h3 {
  margin: 0 0 10px;
  color: var(--ink);
  font-size: 15px;
}

.question-groups button {
  display: block;
  width: 100%;
  height: auto;
  min-height: 36px;
  margin-top: 8px;
  border: 1px solid #28435f;
  background: #09182a;
  color: var(--ink);
  padding: 8px 10px;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
}

.question-groups button:hover {
  border-color: var(--accent);
  color: #fff;
}

.login-body {
  display: grid;
  min-height: 100vh;
  place-items: center;
}

.login-shell {
  width: min(420px, calc(100vw - 32px));
}

.login-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 26px;
}

.login-form {
  display: grid;
  gap: 14px;
  margin-top: 22px;
}

.login-form label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.login-error {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.admin-shell {
  min-height: 100vh;
  padding: 24px;
}

.admin-grid {
  display: grid;
  grid-template-columns: minmax(320px, 480px) minmax(0, 1fr);
  gap: 16px;
}

.admin-form,
.admin-list,
.usage-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 18px;
}

.admin-form h2,
.admin-list h2,
.usage-section h2 {
  margin: 0 0 16px;
  font-size: 18px;
}

.usage-section {
  margin-top: 16px;
}

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.usage-subtitle {
  margin-top: 18px !important;
}

.ai-form {
  margin-bottom: 16px;
}

.ai-config-table {
  margin-top: 12px;
}

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

.form-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.form-grid label:nth-last-child(-n+5),
.form-grid .check-label {
  grid-column: span 2;
}

select {
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #09182a;
  color: var(--ink);
  padding: 0 12px;
  font: inherit;
}

.check-label {
  display: flex !important;
  align-items: center;
  gap: 8px !important;
}

.check-label input {
  width: 16px;
  height: 16px;
}

.admin-actions {
  display: flex;
  gap: 10px;
}

.table-button {
  height: 30px;
  margin-right: 6px;
  padding: 0 10px;
  font-size: 12px;
}

.danger-button {
  background: #7d3430;
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 1050px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .chat-panel {
    min-height: 420px;
    max-height: none;
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .charts-row {
    grid-template-columns: 1fr;
    height: auto;
  }

  .visual-area {
    grid-template-rows: auto minmax(260px, 1fr);
  }

  .chart-wrap {
    height: 320px;
  }
}

@media (max-width: 720px) {
  .workspace {
    padding: 14px;
  }

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

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

  .visual-area {
    grid-template-rows: 320px 360px;
  }

  .question-groups {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 420px) {
  .kpi-grid {
    gap: 8px;
  }

  .kpi-grid article {
    padding: 12px 10px;
    min-height: 82px;
  }

  .kpi-grid span {
    font-size: 12px;
  }

  .kpi-grid strong {
    font-size: 16px;
  }
}

@media (max-width: 340px) {
  .kpi-grid article {
    padding-right: 8px;
    padding-left: 8px;
  }

  .kpi-grid strong {
    font-size: 14px;
  }
}
