:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --border: #e2e4e9;
  --border-strong: #d0d3da;
  --text: #1c1e26;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;
  --accent: #3454d1;
  --accent-hover: #2941ab;
  --accent-soft: #eaeefb;
  --danger: #c0392b;
  --danger-hover: #a52f22;
  --danger-soft: #fbeceb;
  --success: #1f9d55;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(20, 22, 30, 0.06);
  --shadow-md: 0 4px 16px rgba(20, 22, 30, 0.10);
  --shadow-lg: 0 12px 40px rgba(20, 22, 30, 0.18);
  --sidebar-w: 280px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14151a;
    --surface: #1c1e26;
    --surface-raised: #23252f;
    --border: #2d3039;
    --border-strong: #3a3d48;
    --text: #eceef2;
    --text-muted: #9aa0ae;
    --text-faint: #6b7280;
    --accent: #6d87e8;
    --accent-hover: #8299ed;
    --accent-soft: #232a45;
    --danger: #e0685b;
    --danger-hover: #e88378;
    --danger-soft: #3a2224;
    --success: #3fbf72;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  font-size: 14px;
  line-height: 1.5;
}

button {
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}

input, select {
  font-family: inherit;
  font-size: 14px;
}

#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */

#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
}

#sidebar h1 {
  font-size: 17px;
  font-weight: 700;
  margin: 4px 4px 16px;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 8px;
}

#sidebar h1::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}

#profile-switch {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

#profile-switch button {
  flex: 1;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

#profile-switch button[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

#list-select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  margin-bottom: 12px;
  outline: none;
  transition: border-color 0.15s;
}

#list-select:focus {
  border-color: var(--accent);
}

#type-search {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  margin-bottom: 10px;
  outline: none;
  transition: border-color 0.15s;
}

#type-search:focus {
  border-color: var(--accent);
}

#type-sort {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text-muted);
  font-size: 12px;
  outline: none;
  margin-bottom: 10px;
  transition: border-color 0.15s;
}

#type-sort:focus {
  border-color: var(--accent);
}

#type-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.type-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}

.type-item:hover {
  background: var(--bg);
  color: var(--text);
}

.type-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.type-item .name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.type-item-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.type-item .count {
  font-size: 11px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.type-item.active .count {
  color: var(--accent);
  opacity: 0.75;
}

.perf-badge {
  flex-shrink: 0;
  font-size: 10.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 2px 6px;
  border-radius: 999px;
  color: #0b0e14;
  white-space: nowrap;
}

.perf-badge.na {
  background: var(--border-strong);
  color: var(--text-faint);
}

.sidebar-empty {
  color: var(--text-faint);
  font-size: 13px;
  padding: 16px 10px;
  text-align: center;
}

#new-type-btn {
  margin-top: 12px;
  width: 100%;
  padding: 10px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

#new-type-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

#toggle-archived-btn {
  margin-top: 8px;
  width: 100%;
  padding: 6px;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  font-size: 12px;
  font-weight: 600;
}

#toggle-archived-btn:hover {
  color: var(--accent);
}

.type-item.archived {
  opacity: 0.55;
  font-style: italic;
}

/* ---------- Main content ---------- */

#content {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

#empty-state, #types-empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-faint);
  max-width: 320px;
  padding: 24px;
}

#empty-state .icon, #types-empty-state .icon {
  width: 40px;
  height: 40px;
  margin: 0 auto 14px;
  color: var(--text-faint);
  opacity: 0.6;
}

#empty-state p, #types-empty-state p {
  margin: 0;
  font-size: 14px;
}

#table-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}

#table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 22px 28px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: nowrap;
}

.header-title {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

#type-name {
  font-size: 20px;
  font-weight: 700;
  margin: 0;
  min-width: 0;
  letter-spacing: -0.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-count {
  font-size: 12.5px;
  color: var(--text-faint);
  white-space: nowrap;
  flex-shrink: 0;
}

#type-actions {
  display: flex;
  gap: 8px;
  flex-wrap: nowrap;
  flex-shrink: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  transition: background 0.12s, border-color 0.12s, color 0.12s, transform 0.05s;
  white-space: nowrap;
}

.btn:hover { background: var(--bg); border-color: var(--border-strong); }
.btn:active { transform: translateY(1px); }

.btn svg { width: 14px; height: 14px; flex-shrink: 0; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-secondary {
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
}
.btn-secondary:hover { background: var(--bg); color: var(--text); }

.btn-danger { color: var(--danger); border-color: var(--border); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.btn-ghost {
  border-color: transparent;
  background: transparent;
  padding: 6px;
}
.btn-ghost:hover { background: var(--bg); }

.btn-icon-only { padding: 7px; }

/* ---------- Table ---------- */

/* ---------- Chart ---------- */

#chart-section {
  padding: 18px 28px 0;
  flex-shrink: 0;
}

#chart-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

#chart-controls label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

#chart-param-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  outline: none;
}
#chart-param-select:focus { border-color: var(--accent); }

.chart-hint {
  font-size: 12px;
  color: var(--text-faint);
}

#chart-empty, #chart-no-data {
  color: var(--text-faint);
  font-size: 13px;
  padding: 20px;
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
}

#chart-wrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 4px 0;
}

#chart-tooltip {
  position: absolute;
  z-index: 5;
  background: var(--text);
  color: var(--surface);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.4;
  padding: 5px 9px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  pointer-events: none;
  box-shadow: var(--shadow-md, var(--shadow-sm));
}

#chart-tooltip .tooltip-date {
  font-weight: 500;
  opacity: 0.75;
}

#chart-svg {
  width: 100%;
  height: 200px;
  display: block;
  overflow: visible;
}

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

#chart-svg .chart-point {
  fill: var(--surface);
  stroke: var(--accent);
  stroke-width: 2;
  cursor: pointer;
  transition: r 0.1s;
}
#chart-svg .chart-point:hover { r: 5; fill: var(--accent); }

#chart-svg .chart-axis {
  stroke: var(--border);
  stroke-width: 1;
}

#chart-svg .chart-axis-label {
  fill: var(--text-faint);
  font-size: 10.5px;
  font-family: var(--font);
}

#chart-svg .chart-gridline {
  stroke: var(--border);
  stroke-width: 1;
  stroke-dasharray: 3 3;
}

#chart-svg .chart-hit-area {
  fill: transparent;
  cursor: crosshair;
}

#chart-svg .chart-hover-line {
  stroke: var(--accent);
  stroke-width: 1;
  pointer-events: none;
  opacity: 0;
}

#chart-svg .chart-hover-point {
  fill: var(--accent);
  stroke: var(--surface);
  stroke-width: 2;
  pointer-events: none;
  opacity: 0;
}

#chart-svg.chart-hovering .chart-hover-line,
#chart-svg.chart-hovering .chart-hover-point {
  opacity: 1;
}

/* ---------- Column type select (modals) ---------- */

.type-select {
  padding: 8px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.type-select:focus { border-color: var(--accent); }

/* ---------- Enhanced select popup ---------- */
/* Native <select> popups ignore font-family on macOS Safari, so mouse-driven
   opens are replaced with this app-styled menu (keyboard access still uses
   the native popup, which is unaffected). */

select.enhanced-select-open { border-color: var(--accent); }

.enhanced-select-menu {
  position: fixed;
  z-index: 200;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  padding: 4px;
  max-height: 240px;
  overflow-y: auto;
  font-family: var(--font);
  font-size: 13px;
}

.enhanced-select-option {
  padding: 7px 10px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
}
.enhanced-select-option:hover { background: var(--bg); }
.enhanced-select-option.selected { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---------- Column drag-and-drop ---------- */

#log-table th[draggable="true"] {
  cursor: grab;
}

#log-table th.dragging {
  opacity: 0.4;
}

#log-table th.drag-over {
  box-shadow: inset 2px 0 0 var(--accent);
}

#table-wrap {
  flex: 1;
  overflow: auto;
  padding: 0 28px 28px;
}

table#log-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  margin-top: 16px;
  background: var(--surface);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

#log-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface-raised);
  text-align: left;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  z-index: 1;
}

#log-table th .th-inner {
  display: flex;
  align-items: center;
  gap: 6px;
}

.col-type-badge {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-faint);
  background: var(--bg);
  border-radius: 4px;
  padding: 1px 4px;
  line-height: 1.4;
}

td.col-numeric .cell-input {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.cell-with-suffix {
  position: relative;
  display: flex;
  align-items: center;
}

.cell-with-suffix .cell-input {
  padding-right: 22px;
}

.cell-suffix {
  position: absolute;
  right: 10px;
  color: var(--text-faint);
  font-size: 12.5px;
  pointer-events: none;
}

#log-table th .col-actions {
  display: inline-flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.12s;
}

#log-table th:hover .col-actions { opacity: 1; }

#log-table th .col-actions button {
  border: none;
  background: transparent;
  color: var(--text-faint);
  padding: 3px;
  border-radius: 4px;
  display: flex;
}
#log-table th .col-actions button:hover { background: var(--border); color: var(--text); }
#log-table th .col-actions svg { width: 11px; height: 11px; }

#log-table td {
  padding: 0;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

#log-table tbody tr:last-child td { border-bottom: none; }

#log-table tbody tr:hover { background: var(--bg); }

.cell-input {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  padding: 10px 14px;
  outline: none;
  font-size: 13.5px;
  border-radius: 0;
  transition: background 0.12s, border-color 0.12s;
}

.cell-input:hover { background: var(--surface-raised); }

.cell-input:focus {
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.cell-datetime {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  font-size: 12.5px;
  white-space: nowrap;
  padding: 10px 14px;
  display: block;
  cursor: pointer;
  border-radius: 4px;
}
.cell-datetime:hover { background: var(--surface-raised); color: var(--text); }

td.col-datetime, th.col-datetime { width: 1%; }
td.col-actions-cell, th.col-actions-th { width: 1%; }

.row-delete-btn {
  border: none;
  background: transparent;
  color: var(--text-faint);
  padding: 6px;
  margin: 0 8px;
  border-radius: 6px;
  display: flex;
  opacity: 0;
  transition: opacity 0.12s, background 0.12s, color 0.12s;
}

tr:hover .row-delete-btn { opacity: 1; }
.row-delete-btn:hover { background: var(--danger-soft); color: var(--danger); }
.row-delete-btn svg { width: 14px; height: 14px; }

#add-row-inline-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  margin-top: 10px;
  padding: 10px 14px;
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.12s, border-color 0.12s;
}
#add-row-inline-btn:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
#add-row-inline-btn svg { width: 14px; height: 14px; flex-shrink: 0; }

.table-empty-row td {
  text-align: center;
  color: var(--text-faint);
  padding: 40px 14px;
  font-size: 13px;
}

/* ---------- Modal ---------- */

#modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 16, 20, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
  animation: fade-in 0.12s ease-out;
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

#modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 480px;
  max-height: calc(100vh - 64px);
  overflow-y: auto;
  padding: 28px;
  animation: modal-in 0.14s cubic-bezier(.2,.9,.3,1.2);
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal h3 {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 700;
}

.modal .modal-subtitle {
  margin: 0 0 18px;
  font-size: 12.5px;
  color: var(--text-muted);
}

.modal label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.modal input[type="text"] {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  outline: none;
  margin-bottom: 16px;
  transition: border-color 0.15s;
}

.modal input[type="text"]:focus { border-color: var(--accent); }

.modal-field-error {
  font-size: 12px;
  color: var(--danger);
  margin: -12px 0 14px;
  min-height: 0;
}

.column-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 18px;
}

.column-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.column-list-item input {
  flex: 1;
  min-width: 0;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.column-list-item input:focus { border-color: var(--accent); }

.column-list-item .type-select {
  flex-shrink: 0;
  width: 132px;
}

.column-list-item button {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-faint);
  border-radius: var(--radius-sm);
  font-size: 16px;
  line-height: 1;
  transition: background 0.12s, color 0.12s;
}
.column-list-item button:hover { background: var(--danger-soft); color: var(--danger); }

.add-column-row {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  padding: 6px 2px;
}
.add-column-row svg { width: 13px; height: 13px; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

.modal-actions .btn {
  min-width: 92px;
  padding: 9px 16px;
}

.modal-body-text {
  font-size: 13.5px;
  color: var(--text-muted);
  margin: 0 0 20px;
  line-height: 1.6;
}

.modal-body-text strong { color: var(--text); }

/* ---------- Toasts ---------- */

#toast-stack {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--danger);
  color: var(--text);
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: 13px;
  max-width: 320px;
  animation: toast-in 0.15s ease-out;
}

.toast.success { border-left-color: var(--success); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Responsive ---------- */

#sidebar-toggle { display: none; }

@media (max-width: 760px) {
  :root { --sidebar-w: 240px; }

  #sidebar-toggle {
    display: inline-flex;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 7px 9px;
    margin-right: 4px;
  }

  #app { position: relative; }

  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.18s ease;
    box-shadow: var(--shadow-lg);
  }

  #sidebar.open { transform: translateX(0); }

  #sidebar-scrim {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.35);
    z-index: 30;
  }
  #sidebar-scrim.visible { display: block; }

  #table-header { padding: 16px; }
  #chart-section { padding: 14px 16px 0; }
  #table-wrap { padding: 0 16px 16px; }
  #type-name { font-size: 17px; }
}
