:root {
  --bg: #f3f5f2;
  --panel: #ffffff;
  --ink: #1c2420;
  --muted: #5c6b63;
  --line: #d5ddd7;
  --accent: #0f5c4c;
  --accent-soft: #e4f2ed;
  --danger: #8b1e1e;
  --danger-bg: #f8e8e8;
  --warn: #7a5a00;
  --warn-bg: #fff6d9;
  --override: #14457f;
  --override-bg: #dbe8fb;
  --ok: #0f5c4c;
  --font: "IBM Plex Sans", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 500px at 10% -10%, #d9ebe4 0%, transparent 55%),
    linear-gradient(180deg, #eef2ef 0%, var(--bg) 40%);
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.9rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(6px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand-link {
  color: var(--ink);
  text-decoration: none;
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
}

.brand-mark {
  width: 28px;
  height: 28px;
  display: block;
  border-radius: 7px;
  box-shadow: 0 1px 2px rgba(15, 92, 76, 0.25);
}

.brand-word {
  display: inline-flex;
  align-items: baseline;
  gap: 0.35rem;
}

.brand-sub {
  color: var(--muted);
  font-weight: 500;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.login-mark {
  width: 72px;
  height: 72px;
  display: block;
  margin: 0 auto 0.9rem;
  border-radius: 18px;
  box-shadow: 0 6px 18px rgba(15, 92, 76, 0.28);
}

.session {
  position: relative;
}

.user-badge {
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 0.82rem;
  letter-spacing: 0.02em;
  cursor: pointer;
  display: grid;
  place-items: center;
  overflow: hidden;
  flex-shrink: 0;
}

.user-badge:hover { filter: brightness(1.05); }

.user-badge img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.user-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 0.55rem);
  min-width: 260px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(28, 36, 32, 0.14);
  padding: 0.35rem 0 0.45rem;
  z-index: 30;
}

.user-menu-identity {
  padding: 0.7rem 0.95rem 0.8rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 0.3rem;
}

.user-menu-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.user-menu-email,
.user-menu-role {
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 0.15rem;
}

.user-menu-item {
  display: block;
  width: 100%;
  box-sizing: border-box;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 0;
  color: var(--ink);
  padding: 0.55rem 0.95rem;
  font: inherit;
  font-size: 0.9rem;
  text-decoration: none;
  cursor: pointer;
}

.user-menu-item:hover {
  background: var(--accent-soft);
  color: var(--ink);
  filter: none;
}

main {
  padding: 1.25rem;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.foot {
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0.55rem 1.25rem;
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(6px);
}

.foot a {
  color: var(--accent);
  font-size: 0.82rem;
  text-decoration: none;
}

.foot a:hover {
  text-decoration: underline;
}

#app-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel, .detail-pane, .list-pane {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem 1.1rem;
}

.hidden { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  align-items: end;
  margin-bottom: 0.75rem;
}

.toolbar label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.toolbar label.check {
  flex-direction: row;
  align-items: center;
  padding-bottom: 0.35rem;
}

.tag-filter-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.75rem;
  margin: 0 0 0.85rem;
}

button.linkish {
  background: none;
  border: none;
  color: var(--accent);
  padding: 0;
  font-size: 0.8rem;
  text-decoration: underline;
  cursor: pointer;
}

.tag-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
}

button.tag-chip {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.28rem 0.85rem;
  font-size: 0.82rem;
  line-height: 1.2;
}

button.tag-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
  filter: none;
}

button.tag-chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

select, button {
  font: inherit;
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

button {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  cursor: pointer;
}

button.secondary {
  background: #fff;
  color: var(--accent);
  border-color: var(--accent);
}

button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

button:hover { filter: brightness(1.05); }

.detail-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin: 0.5rem 0 0.75rem;
}

.ok-banner {
  color: var(--ok);
  background: var(--accent-soft);
  border: 1px solid #b7d9ce;
  padding: 0.6rem 0.75rem;
  border-radius: 6px;
}

dialog {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0;
  max-width: min(640px, 94vw);
  width: 100%;
  color: var(--ink);
  background: var(--panel);
}

dialog::backdrop {
  background: rgba(20, 30, 26, 0.45);
}

#kpi-form,
#confirm-delete-form {
  padding: 1rem 1.15rem 1.15rem;
}

#kpi-form h2,
#confirm-delete-form h2 {
  margin: 0 0 0.75rem;
  font-size: 1.15rem;
}

#confirm-delete-copy {
  margin: 0 0 0.5rem;
  line-height: 1.4;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.form-grid .span-2 { grid-column: 1 / -1; }

.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.form-grid label.check {
  flex-direction: row;
  align-items: center;
  gap: 0.45rem;
}

/* `display: flex` above would otherwise defeat the hidden attribute. */
.form-grid [hidden] { display: none; }

.form-grid input,
.form-grid textarea,
.form-grid select {
  font: inherit;
  padding: 0.4rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #fff;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

.preview {
  margin: 0.75rem 0 0;
  padding: 0.65rem 0.75rem;
  background: #f0f4f1;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 0.78rem;
  overflow: auto;
  max-height: 220px;
}

@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
}

.row { display: flex; gap: 0.75rem; flex-wrap: wrap; margin-top: 1rem; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, 0.9fr);
  gap: 1rem;
  flex: 1;
  min-height: 0;
}

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

.list-pane,
.detail-pane {
  min-height: 0;
  overflow: hidden;
}

.list-pane {
  display: flex;
  flex-direction: column;
  padding: 0;
}

.list-pane-bar {
  position: relative;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.55rem 0.15rem;
  flex-shrink: 0;
  z-index: 5;
}

.kpi-name-filter {
  flex: 1;
  min-width: 0;
}

.kpi-name-filter input {
  width: 100%;
  box-sizing: border-box;
  font: inherit;
  padding: 0.35rem 0.55rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

.icon-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  position: relative;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  display: block;
}

.icon-btn:hover,
.icon-btn[aria-expanded="true"] {
  color: var(--accent);
  border-color: var(--accent);
  filter: none;
}

.icon-btn.has-filters::after {
  content: "";
  position: absolute;
  top: 5px;
  right: 5px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px #fff;
}

.icon-btn-tip {
  position: absolute;
  right: calc(100% + 0.45rem);
  top: 50%;
  transform: translateY(-50%);
  background: var(--ink);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  padding: 0.22rem 0.5rem;
  border-radius: 999px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.12s ease;
  z-index: 9;
  box-shadow: 0 2px 8px rgba(28, 36, 32, 0.25);
}

.icon-btn:hover .icon-btn-tip,
.icon-btn:focus-visible .icon-btn-tip {
  opacity: 1;
  visibility: visible;
}

.icon-btn[aria-expanded="true"] .icon-btn-tip {
  opacity: 0;
  visibility: hidden;
}

.filter-menu {
  position: absolute;
  right: 0.55rem;
  top: calc(100% - 0.1rem);
  z-index: 8;
  min-width: 220px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(28, 36, 32, 0.14);
  padding: 0.75rem 0.85rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.filter-menu label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.filter-menu select {
  width: 100%;
}

.table-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0 1.1rem 1rem;
}

.detail-pane {
  overflow: auto;
}

#kpi-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.92rem;
}

#kpi-table th,
#kpi-table td {
  text-align: left;
  padding: 0.55rem 0.4rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

#kpi-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--panel);
  box-shadow: 0 1px 0 var(--line);
}

.sort-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.28rem;
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  cursor: pointer;
}

.sort-btn:hover {
  color: var(--ink);
}

.sort-btn::after {
  content: "↕";
  font-size: 0.72rem;
  font-weight: 500;
  opacity: 0.35;
  text-transform: none;
}

th[aria-sort="ascending"] .sort-btn::after {
  content: "↑";
  opacity: 1;
  color: var(--accent);
}

th[aria-sort="descending"] .sort-btn::after {
  content: "↓";
  opacity: 1;
  color: var(--accent);
}

.col-grain, .col-owner { white-space: nowrap; }

.col-delete {
  width: 2.4rem;
  padding-left: 0.2rem;
  padding-right: 0.15rem;
}

.row-delete {
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.row-delete svg {
  width: 15px;
  height: 15px;
  display: block;
}

.row-delete:hover {
  color: var(--danger);
  background: var(--danger-bg);
  filter: none;
}

.value-cell.editable { cursor: text; }

.value-input {
  width: 100%;
  max-width: 8rem;
  font-family: var(--mono);
  font-size: 0.92rem;
  padding: 0.15rem 0.3rem;
  border: 1px solid var(--accent);
  border-radius: 4px;
  background: #fff;
  color: var(--ink);
}

.col-spark {
  width: 96px;
  vertical-align: middle;
}

.kpi-spark {
  width: 88px;
  height: 28px;
  display: block;
}

.kpi-spark polyline {
  fill: none;
  stroke: var(--accent);
  stroke-width: 1.75;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.kpi-spark.made polyline { stroke: var(--ok); }
.kpi-spark.missed polyline { stroke: var(--danger); }

.kpi-spark .spark-target {
  stroke: var(--muted);
  stroke-width: 1;
  stroke-dasharray: 3 2;
}

#kpi-table tbody tr { cursor: pointer; }
#kpi-table tbody tr:hover,
#kpi-table tbody tr.active { background: var(--accent-soft); }

.muted { color: var(--muted); }
.error {
  color: var(--danger);
  background: var(--danger-bg);
  border: 1px solid #efc4c4;
  padding: 0.6rem 0.75rem;
  border-radius: 6px;
}

.pill {
  display: inline-block;
  padding: 0.12rem 0.55rem;
  border-radius: 999px;
  background: #eef2ef;
  color: var(--muted);
  font-size: 0.75rem;
  margin-right: 0.2rem;
  line-height: 1.35;
}

.value-ok { color: var(--ok); font-family: var(--mono); }
.value-made { color: var(--ok); font-family: var(--mono); font-weight: 600; }
.value-missed { color: var(--danger); font-family: var(--mono); font-weight: 600; }
.value-empty { color: var(--warn); background: var(--warn-bg); padding: 0.1rem 0.35rem; border-radius: 4px; }
.value-error { color: var(--danger); font-family: var(--mono); white-space: pre-wrap; }

/* Manually overridden readings read blue so they are never mistaken for generated numbers. */
.value-override {
  color: var(--override);
  background: var(--override-bg);
  font-family: var(--mono);
  font-weight: 600;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

.value-wrap {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
}

button.value-restore {
  background: none;
  border: none;
  padding: 0;
  color: var(--override);
  font-size: 0.72rem;
  text-decoration: underline;
  cursor: pointer;
}

button.value-restore:hover {
  filter: none;
  color: var(--ink);
}

.override-note { font-size: 0.78rem; margin-top: 0.25rem; }

.detail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.detail-head h2 { margin: 0; }

.form-note { font-size: 0.78rem; margin: 0; }

.detail-pane h2 { margin: 0 0 0.35rem; font-size: 1.2rem; }
.detail-pane dl { margin: 0.75rem 0; }
.detail-pane dt { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 0.65rem; }
.detail-pane dd { margin: 0.15rem 0 0; }

#situation-root {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.situation-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
}

.situation-body {
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: 0.95rem;
}

.situation-cache-note {
  margin: 0.75rem 0 0;
  font-size: 0.75rem;
}

.situation-body.streaming::after {
  content: "▍";
  margin-left: 1px;
  animation: situation-caret 1s steps(1) infinite;
}

.kpi-mention {
  display: inline;
  padding: 0;
  margin: 0;
  border: 0;
  background: none;
  font: inherit;
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  cursor: pointer;
}

.kpi-mention:hover {
  color: var(--ink);
  background: var(--accent-soft);
}

.situation-chat {
  display: grid;
  gap: 0.6rem;
  margin-top: 1rem;
}

.chat-message {
  max-width: 94%;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  line-height: 1.4;
  white-space: pre-wrap;
  font-size: 0.9rem;
}

.chat-user {
  justify-self: end;
  background: var(--accent);
  color: #fff;
}

.chat-assistant {
  justify-self: start;
  background: var(--accent-soft);
  color: var(--ink);
}

.chat-role {
  display: block;
  margin-bottom: 0.15rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.75;
}

.chat-thinking {
  color: var(--muted);
  font-style: italic;
}

.situation-chat-form {
  flex: 0 0 auto;
  display: flex;
  gap: 0.45rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--line);
  margin-top: 0.75rem;
}

.situation-chat-form input {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--ink);
}

.situation-chat-form input:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.situation-chat-form button:disabled,
.situation-chat-form input:disabled {
  opacity: 0.6;
  cursor: wait;
}

@keyframes situation-caret {
  50% {
    opacity: 0;
  }
}

.history {
  width: 100%;
  margin-top: 0.5rem;
  font-family: var(--mono);
  font-size: 0.85rem;
  border-collapse: collapse;
}

.history-chart-wrap {
  margin: 0.15rem 0 0.85rem;
}

.history-chart {
  width: 100%;
  height: auto;
  display: block;
}

.chart-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.25;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.chart-area {
  fill: var(--accent-soft);
  stroke: none;
}

.chart-target {
  stroke: var(--muted);
  stroke-width: 1;
  stroke-dasharray: 4 3;
}

.chart-label {
  fill: var(--muted);
  font-size: 8px;
  font-family: var(--mono);
}

.history th,
.history td {
  text-align: left;
  padding: 0.35rem 0.4rem;
  border-bottom: 1px solid var(--line);
}
