/* --- Light Theme --- */
:root {
  color-scheme: light;
  --bg: #f0f2f5;
  --surface: #ffffff;
  --text: #1a1d21;
  --muted: #65676b;
  --line: #e0e3e8;
  --accent: #147d64;
  --accent-2: #305cde;
  --danger: #b42318;
  --hover-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  --skeleton-bg: #e8eaed;
  --skeleton-shine: #f5f6f7;
  --chart-input: rgba(48, 92, 222, 0.5);
  --chart-output: rgba(20, 125, 100, 0.5);
  --chart-input-border: #305cde;
  --chart-output-border: #147d64;
  --row-alt: #f8f9fa;
}

/* --- Dark Theme --- */
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #18191a;
  --surface: #242526;
  --text: #e4e6eb;
  --muted: #b0b3b8;
  --line: #3e4042;
  --accent: #2dd4a8;
  --accent-2: #6b9fff;
  --danger: #f25c54;
  --hover-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  --skeleton-bg: #3a3b3c;
  --skeleton-shine: #4a4b4c;
  --chart-input: rgba(107, 159, 255, 0.4);
  --chart-output: rgba(45, 212, 168, 0.4);
  --chart-input-border: #6b9fff;
  --chart-output-border: #2dd4a8;
  --row-alt: #2a2b2c;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #18191a;
    --surface: #242526;
    --text: #e4e6eb;
    --muted: #b0b3b8;
    --line: #3e4042;
    --accent: #2dd4a8;
    --accent-2: #6b9fff;
    --danger: #f25c54;
    --hover-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    --skeleton-bg: #3a3b3c;
    --skeleton-shine: #4a4b4c;
    --chart-input: rgba(107, 159, 255, 0.4);
    --chart-output: rgba(45, 212, 168, 0.4);
    --chart-input-border: #6b9fff;
    --chart-output-border: #2dd4a8;
    --row-alt: #2a2b2c;
  }
}

* {
  box-sizing: border-box;
}

[v-cloak] {
  display: none;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background 0.3s, color 0.3s;
}

button,
input,
select {
  font: inherit;
}

/* --- Layout --- */
.shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.topbar-left p {
  color: var(--muted);
  margin-top: 4px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 32px;
  line-height: 1.1;
  font-weight: 700;
}

h2 {
  font-size: 18px;
  font-weight: 600;
}

/* --- Controls --- */
.controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

select,
input,
button {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0 12px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

select:hover,
input:hover,
button:hover {
  border-color: var(--accent);
}

select:focus,
input:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  background: var(--text);
  color: var(--surface);
  cursor: pointer;
  font-weight: 500;
}

button:hover {
  opacity: 0.88;
}

/* --- Theme Toggle --- */
.theme-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  width: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-icon {
  display: inline-block;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text);
  transition: transform 0.3s, box-shadow 0.3s;
  box-shadow: inset -4px 0 0 0 var(--surface);
}

[data-theme="dark"] .theme-icon {
  box-shadow: inset -6px 0 0 0 var(--surface), 0 0 0 2px var(--muted);
  background: var(--muted);
}

/* --- Metric Cards --- */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.metric,
.panel {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  transition: transform 0.2s, box-shadow 0.2s, border-color 0.3s, background 0.3s;
}

.metric {
  min-height: 108px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.metric:hover,
.panel:hover {
  transform: translateY(-2px);
  box-shadow: var(--hover-shadow);
}

.metric span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.metric strong {
  font-size: 28px;
  line-height: 1.15;
  word-break: break-word;
  font-variant-numeric: tabular-nums;
}

/* --- Dimension Tabs --- */
.dim-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 16px;
}

.dim-tabs button {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--line);
  height: 30px;
  padding: 0 14px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 999px;
}

.dim-tabs button:hover {
  border-color: var(--accent);
  color: var(--text);
}

.dim-tabs button.active {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}

/* --- Chart --- */
.chart-panel {
  margin-bottom: 16px;
  padding: 20px;
}

.chart-wrap {
  position: relative;
  height: 260px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.panel-head span {
  color: var(--muted);
  font-size: 14px;
}

/* --- Rank --- */
.rank-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.panel {
  padding: 18px;
}

.rank-list {
  display: grid;
  gap: 12px;
}

.rank-row {
  display: grid;
  grid-template-columns: 28px minmax(60px, 1fr) minmax(100px, 2fr) auto;
  gap: 10px;
  align-items: center;
}

.rank-idx {
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}

.rank-idx.top {
  color: var(--accent);
}

.rank-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.rank-track {
  height: 8px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.rank-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.6s ease;
}

.cost-fill {
  background: linear-gradient(90deg, #e67e22, var(--accent));
}

.rank-row strong {
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* --- Heatmap --- */
.heatmap-panel {
  --heatmap-cell-size: 13px;
  --heatmap-cell-gap: 3px;
  margin-bottom: 16px;
  padding: 18px 20px;
  position: relative;
}

.activity-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 32px;
  min-height: 142px;
}

.activity-summary {
  flex: 1 1 360px;
  display: grid;
  grid-template-columns: repeat(2, minmax(140px, 1fr));
  gap: 12px;
}

.activity-stat {
  min-height: 58px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--row-alt);
}

.activity-stat span,
.activity-stat small {
  display: block;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.activity-stat strong {
  display: block;
  margin: 5px 0 3px;
  font-size: 20px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.activity-heatmap {
  flex: 0 0 auto;
  max-width: 55%;
}

.heatmap-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
}

.heatmap-wrapper {
  width: max-content;
  margin-left: auto;
}

.heatmap-container {
  display: flex;
  gap: 8px;
  width: max-content;
}

.heatmap-labels {
  display: grid;
  grid-template-rows: repeat(7, var(--heatmap-cell-size));
  gap: var(--heatmap-cell-gap);
  flex-shrink: 0;
  padding-top: 22px;
}

.heatmap-labels span {
  font-size: 11px;
  color: var(--muted);
  line-height: var(--heatmap-cell-size);
  text-align: right;
}

.heatmap-grid-wrapper {
  display: inline-flex;
  flex: 0 0 auto;
  flex-direction: column;
  min-width: 0;
  width: max-content;
}

.heatmap-months {
  display: grid;
  grid-auto-columns: var(--heatmap-cell-size);
  grid-auto-flow: column;
  column-gap: var(--heatmap-cell-gap);
  height: 16px;
  margin-bottom: 6px;
}

.heatmap-months span {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.heatmap-grid {
  display: grid;
  grid-template-rows: repeat(7, var(--heatmap-cell-size));
  grid-auto-flow: column;
  grid-auto-columns: var(--heatmap-cell-size);
  gap: var(--heatmap-cell-gap);
}

.heatmap-cell {
  width: var(--heatmap-cell-size);
  height: var(--heatmap-cell-size);
  border-radius: 2px;
  background: var(--line);
  cursor: pointer;
  transition: transform 0.1s;
}

.heatmap-cell:hover {
  transform: scale(1.4);
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  z-index: 2;
  position: relative;
}

.heat-level-0 { background: var(--line); }
.heat-level-1 { background: #c6ede3; }
.heat-level-2 { background: #73cfba; }
.heat-level-3 { background: #2fb898; }
.heat-level-4 { background: #147d64; }

[data-theme="dark"] .heat-level-1 { background: #1e3d35; }
[data-theme="dark"] .heat-level-2 { background: #1a5c4c; }
[data-theme="dark"] .heat-level-3 { background: #228a6f; }
[data-theme="dark"] .heat-level-4 { background: #2dd4a8; }

.heatmap-tooltip {
  position: absolute;
  z-index: 10;
  background: var(--text);
  color: var(--surface);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.5;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transform: translate(-50%, -100%);
  margin-top: -10px;
}

.heatmap-tooltip strong {
  display: block;
  font-weight: 600;
}

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 10px;
  justify-content: flex-end;
  width: 100%;
}

.legend-label {
  font-size: 11px;
  color: var(--muted);
}

.legend-cell {
  cursor: default;
}

.legend-cell:hover {
  transform: none;
  outline: none;
}

/* --- Table --- */
.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 800px;
}

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

th {
  color: var(--text);
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

tbody tr:nth-child(even) {
  background: var(--row-alt);
}

tbody tr {
  transition: background 0.15s;
}

tbody tr:hover {
  background: var(--line);
}

.empty {
  color: var(--muted);
  padding: 24px 0;
  text-align: center;
}

/* --- Skeleton --- */
.skeleton-text {
  color: transparent !important;
  background: var(--skeleton-bg);
  border-radius: 6px;
  min-width: 60px;
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% { background-position: -200px 0; }
  100% { background-position: 200px 0; }
}

.skeleton-text {
  background: linear-gradient(90deg, var(--skeleton-bg) 25%, var(--skeleton-shine) 50%, var(--skeleton-bg) 75%);
  background-size: 400px 100%;
}

.skeleton-row td {
  padding: 14px 10px;
}

.skeleton-bar {
  height: 14px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--skeleton-bg) 25%, var(--skeleton-shine) 50%, var(--skeleton-bg) 75%);
  background-size: 400px 100%;
  animation: shimmer 1.5s infinite;
}

/* --- Footer --- */
.footer {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* --- Toast --- */
.usage-toast-container {
  position: fixed;
  top: 20px;
  left: 50%;
  z-index: 9999;
  width: min(520px, calc(100vw - 32px));
  pointer-events: none;
  transform: translateX(-50%);
}

.usage-toast-card {
  --toast-fade-ms: 1000ms;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
  background: rgba(32, 32, 32, 0.96);
  color: #e5e7eb;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
  padding: 14px 14px 14px 16px;
  pointer-events: auto;
  backdrop-filter: blur(12px);
}

[data-theme="light"] .usage-toast-card {
  border-color: rgba(26, 29, 33, 0.12);
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  box-shadow: 0 16px 40px rgba(23, 32, 38, 0.18);
}

.usage-toast-content {
  display: grid;
  gap: 5px;
  min-width: 0;
  flex: 1;
  font-size: 13px;
  line-height: 1.45;
}

.usage-toast-content strong {
  color: inherit;
  font-size: 14px;
  line-height: 1.25;
}

.usage-toast-content span {
  color: var(--muted);
  overflow-wrap: anywhere;
}

[data-theme="dark"] .usage-toast-content span,
:root:not([data-theme="light"]) .usage-toast-content span {
  color: #cbd5e1;
}

.usage-toast-enter-active,
.usage-toast-leave-active {
  transition:
    opacity var(--toast-fade-ms) ease,
    transform var(--toast-fade-ms) ease;
}

.usage-toast-enter-from,
.usage-toast-leave-to {
  opacity: 0;
  transform: translateY(-12px);
}

.toast-root {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 20;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 32px));
}

.toast {
  border: 1px solid #f4c7c3;
  border-radius: 10px;
  background: #fff7f6;
  color: var(--danger);
  padding: 12px 14px;
  box-shadow: 0 12px 28px rgba(23, 32, 38, 0.16);
  font-size: 14px;
  line-height: 1.4;
  animation: toast-in 0.3s ease;
}

[data-theme="dark"] .toast {
  background: #2a1a1a;
  border-color: #5c2020;
}

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

/* --- Responsive --- */
@media (max-width: 980px) {
  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .rank-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 768px) {
  .activity-content {
    align-items: stretch;
    flex-direction: column;
    gap: 16px;
  }
  .activity-summary {
    flex-basis: auto;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .activity-heatmap {
    max-width: 100%;
  }
  .heatmap-wrapper {
    margin-left: 0;
  }
}

@media (max-width: 640px) {
  .shell {
    padding: 16px;
  }
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }
  .metric-grid,
  .rank-grid {
    grid-template-columns: 1fr;
  }
  .controls {
    width: 100%;
  }
  .controls select,
  .controls button {
    flex: 1;
  }
  .controls .theme-btn {
    flex: 0 0 36px;
  }
  .toast-root {
    right: 16px;
    bottom: 16px;
  }
  .usage-toast-container {
    top: 12px;
    width: calc(100vw - 24px);
  }
  .usage-toast-card {
    padding: 12px;
  }
  .heatmap-panel {
    --heatmap-cell-size: 11px;
    --heatmap-cell-gap: 2px;
  }
  .activity-summary {
    grid-template-columns: 1fr;
  }
}
