:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f0f3f9;
  --border: #e3e8f1;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-text: #ffffff;
  --pos: #059669;
  --pos-soft: #e6f7f0;
  --neg: #dc2626;
  --neg-soft: #fdecec;
  --warn: #d97706;
  --warn-soft: #fff4e0;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 8px 24px rgba(15, 23, 42, .06);
  --radius: 16px;
  --radius-sm: 10px;
  --grid: rgba(100, 116, 139, .15);
  font: 15px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0b1020;
    --surface: #131a2e;
    --surface-2: #1a2340;
    --border: #25304f;
    --text: #e7ecf8;
    --muted: #8a97b5;
    --accent: #818cf8;
    --accent-soft: #1f2750;
    --accent-text: #0b1020;
    --pos: #34d399;
    --pos-soft: #0f2d28;
    --neg: #f87171;
    --neg-soft: #34181c;
    --warn: #fbbf24;
    --warn-soft: #33270f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
    --grid: rgba(138, 151, 181, .15);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; letter-spacing: -.02em; line-height: 1.2; }
h2 { font-size: 1.35rem; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
[hidden] { display: none !important; }

.shell { max-width: 1180px; margin: 0 auto; padding: 1.25rem 1.25rem 3rem; }

/* Topbar */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 1rem; padding: .5rem 0 1.25rem;
}
.brand { display: flex; align-items: center; gap: .8rem; }
.logo {
  width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center;
  background: linear-gradient(135deg, #6366f1, #8b5cf6 55%, #ec4899); color: #fff;
  box-shadow: 0 6px 16px rgba(99, 102, 241, .35);
}
.brand h1 { font-size: 1.25rem; }
.brand .muted { font-size: .8rem; }
.userbox { display: flex; align-items: center; gap: .7rem; }
.role-pill {
  font-size: .78rem; font-weight: 600; padding: .25rem .65rem; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}

/* Navigation */
nav {
  display: flex; gap: .25rem; padding: .3rem; width: fit-content; max-width: 100%;
  overflow-x: auto; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); margin-bottom: 1.5rem;
}
nav button {
  border: 0; background: transparent; color: var(--muted); font-weight: 600;
  padding: .55rem 1rem; border-radius: 10px; white-space: nowrap;
}
nav button:hover { color: var(--text); background: var(--surface-2); }
nav button[aria-current="page"] { background: var(--accent); color: var(--accent-text); }

/* Controls */
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; transition: background .15s, border-color .15s, transform .05s; }
button:active { transform: translateY(1px); }
button:focus-visible, select:focus-visible, input:focus-visible, nav button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
select, input[type="text"], input[type="password"], input[type="number"], input[type="datetime-local"] {
  width: 100%; padding: .6rem .75rem; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}
select:focus, input:focus { border-color: var(--accent); }
select, input[type="text"], input[type="password"], input[type="number"], input[type="datetime-local"] { min-height: 2.7rem; }
.btn {
  background: var(--accent); color: var(--accent-text); border: 0; font-weight: 600;
  padding: .65rem 1.2rem; border-radius: var(--radius-sm);
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-ghost {
  background: transparent; color: var(--text); border: 1px solid var(--border);
  padding: .45rem .9rem; border-radius: var(--radius-sm); font-weight: 600;
}
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { color: var(--neg); }
.btn-danger:hover { background: var(--neg-soft); border-color: transparent; }

/* Cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem;
}
.card + .card { margin-top: 1.25rem; }
.card-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: .8rem; margin-bottom: 1rem;
}
.page-head { margin-bottom: 1.25rem; }
.page-head p { margin-top: .25rem; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.1rem 1.25rem;
}
.kpi .label { color: var(--muted); font-size: .78rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.kpi .value { font-size: 1.75rem; font-weight: 700; letter-spacing: -.03em; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .85rem; margin-top: .1rem; }
.kpi.hero { background: linear-gradient(135deg, #4f46e5, #7c3aed 60%, #db2777); border: 0; color: #fff; }
.kpi.hero .label, .kpi.hero .sub { color: rgba(255, 255, 255, .8); }

/* Segmented range control */
.segmented { display: inline-flex; padding: .2rem; background: var(--surface-2); border-radius: 12px; gap: .15rem; }
.segmented button { border: 0; background: transparent; color: var(--muted); font-weight: 600; padding: .35rem .85rem; border-radius: 9px; }
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.chip span {
  display: inline-flex; align-items: center; gap: .45rem; padding: .35rem .8rem;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface-2);
  color: var(--muted); font-weight: 600; font-size: .88rem; transition: all .15s;
}
.chip i { width: .65rem; height: .65rem; border-radius: 50%; display: inline-block; background: var(--dot, var(--accent)); opacity: .5; }
.chip input:checked + span { color: var(--text); background: var(--surface); border-color: var(--dot, var(--accent)); box-shadow: 0 0 0 1px var(--dot, var(--accent)) inset; }
.chip input:checked + span i { opacity: 1; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.dot-0 { --dot: #6366f1; } .dot-1 { --dot: #f59e0b; } .dot-2 { --dot: #10b981; }
.dot-3 { --dot: #ec4899; } .dot-4 { --dot: #06b6d4; } .dot-5 { --dot: #8b5cf6; } .dot-6 { --dot: #ef4444; }

.chart-wrap { position: relative; height: 380px; }
.legend-row { display: flex; flex-wrap: wrap; gap: .5rem 1rem; margin-top: .8rem; font-size: .82rem; color: var(--muted); }
.legend-row .mk { display: inline-grid; place-items: center; width: 1.2rem; height: 1.2rem; border-radius: 50%; color: #fff; font-size: .7rem; font-weight: 700; margin-right: .3rem; }
.mk-buy { background: #16a34a; } .mk-sell { background: #dc2626; } .mk-swap { background: #d97706; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -.25rem; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th {
  text-align: right; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); font-weight: 600; padding: .6rem .8rem; border-bottom: 1px solid var(--border); white-space: nowrap;
}
td { text-align: right; padding: .8rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
th:nth-child(2), td:nth-child(2) { text-align: left; }
#holdings th:nth-child(1), #holdings td:nth-child(1) { text-align: left; color: var(--muted); width: 2.5rem; }
#trades th:nth-child(1), #trades td:nth-child(1) { text-align: left; }
#trades th:nth-child(6), #trades td:nth-child(6) { text-align: left; max-width: 18rem; overflow: hidden; text-overflow: ellipsis; }
.coin-cell { display: inline-flex; align-items: center; gap: .6rem; }
.coin-avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  font-size: .62rem; font-weight: 700; color: #fff; background: var(--dot, var(--accent));
}
.coin-name { font-weight: 600; }
.coin-sym { color: var(--muted); font-size: .8rem; margin-left: .35rem; }
.badge { display: inline-block; padding: .15rem .6rem; border-radius: 999px; font-size: .78rem; font-weight: 700; }
.badge-buy { background: var(--pos-soft); color: var(--pos); }
.badge-sell { background: var(--neg-soft); color: var(--neg); }
.badge-swap { background: var(--warn-soft); color: var(--warn); }
.pnl-pill { display: inline-block; padding: .15rem .55rem; border-radius: 8px; font-weight: 600; }
.pnl-pill.pos { background: var(--pos-soft); }
.pnl-pill.neg { background: var(--neg-soft); }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }

/* Form */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; align-items: start; }
.form-grid label { display: grid; gap: .35rem; font-size: .85rem; font-weight: 600; color: var(--muted); }
.form-grid .span-all { grid-column: 1 / -1; }
.info-box {
  background: var(--accent-soft); color: var(--text); border-radius: var(--radius-sm);
  padding: .8rem 1rem; font-size: .88rem; margin-bottom: 1.25rem;
}
.type-switch { margin-bottom: 1.25rem; }
.type-switch { display: inline-flex; padding: .2rem; background: var(--surface-2); border-radius: 12px; gap: .15rem; }
.type-switch button { border: 0; background: transparent; color: var(--muted); font-weight: 700; padding: .45rem 1.1rem; border-radius: 9px; }
.type-switch button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }

/* Status toast */
#status:not(:empty) {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .7rem 1.2rem; border-radius: 12px;
  box-shadow: var(--shadow); font-weight: 600; max-width: min(90vw, 560px); z-index: 10;
}
#status.error:not(:empty) { background: var(--neg); color: #fff; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
#loginView {
  width: min(100%, 380px); display: grid; gap: 1.1rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: 20px; box-shadow: var(--shadow); padding: 2rem;
}
#loginView .brand { flex-direction: column; text-align: center; gap: .6rem; }
#loginView .logo { width: 56px; height: 56px; border-radius: 16px; }
#loginView label { display: grid; gap: .35rem; font-size: .85rem; font-weight: 600; color: var(--muted); }
#loginView .btn { width: 100%; padding: .75rem; }
#loginError { min-height: 1.2rem; font-size: .88rem; text-align: center; }

@media (max-width: 640px) {
  .shell { padding: 1rem .8rem 3rem; }
  .kpi .value { font-size: 1.45rem; }
  .chart-wrap { height: 300px; }
  .card { padding: 1rem; }
}
