/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-header: #16213e;
  --text: #e0e0e0;
  --text-muted: #888;
  --primary: #4285f4;
  --green: #34a853;
  --red: #ea4335;
  --orange: #fbbc04;
  --purple: #a855f7;
  --border: #2a2a3e;
  --radius: 12px;
  --nav-height: 64px;
  --header-height: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

[x-cloak] { display: none !important; }

/* === Auth Screen === */
.auth-screen {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}

.auth-container {
  text-align: center;
  padding: 20px;
  width: 100%;
  max-width: 320px;
}

.auth-logo {
  width: 80px; height: 80px;
  background: var(--primary);
  border-radius: 20px;
  display: flex; align-items: center; justify-content: center;
  font-size: 32px; font-weight: 700; color: #fff;
  margin: 0 auto 16px;
}

.auth-container h1 {
  font-size: 20px; font-weight: 600;
  margin-bottom: 8px;
}

.auth-subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 24px;
}

.pin-display {
  display: flex; gap: 12px;
  justify-content: center;
  margin-bottom: 16px;
}

.pin-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid var(--text-muted);
  transition: all .15s;
}

.pin-dot.filled {
  background: var(--primary);
  border-color: var(--primary);
}

.auth-error {
  color: var(--red);
  font-size: 13px;
  margin-bottom: 12px;
  min-height: 20px;
}

.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  max-width: 260px;
  margin: 0 auto;
}

.pin-btn {
  width: 72px; height: 72px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font-size: 24px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: background .15s;
  -webkit-tap-highlight-color: transparent;
  justify-self: center;
}

.pin-btn:active { background: var(--primary); }
.pin-btn.invisible { visibility: hidden; }
.pin-btn.del { font-size: 20px; border: none; background: transparent; }

/* === Header === */
.app-header {
  position: fixed; top: 0; left: 0; right: 0;
  height: calc(var(--header-height) + var(--safe-top));
  padding-top: var(--safe-top);
  background: var(--bg-header);
  display: flex; align-items: center; justify-content: space-between;
  padding-left: 16px; padding-right: 12px;
  z-index: 50;
  border-bottom: 1px solid var(--border);
}

.header-title {
  font-size: 17px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.header-btn {
  background: none; border: none; color: var(--text-muted);
  padding: 8px; cursor: pointer;
}

.header-btn:active { color: var(--text); }

.header-back {
  display: flex; align-items: center; gap: 4px;
  background: none; border: none; color: var(--primary);
  font-size: 15px; cursor: pointer; padding: 8px 4px;
}

/* === Content === */
.app-content {
  padding-top: calc(var(--header-height) + var(--safe-top));
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom));
  min-height: 100vh;
}

.page { padding: 12px; }

/* === KPI Grid === */
.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px;
  border-left: 4px solid var(--border);
}

.kpi-card.blue { border-left-color: var(--primary); }
.kpi-card.green { border-left-color: var(--green); }
.kpi-card.purple { border-left-color: var(--purple); }
.kpi-card.orange { border-left-color: var(--orange); }

.kpi-value {
  font-size: 20px; font-weight: 700;
  margin-bottom: 4px;
}

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

/* === Section & Report Tiles === */
.section-title {
  font-size: 15px; font-weight: 600;
  margin-bottom: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.report-grid {
  display: flex; flex-direction: column; gap: 8px;
}

.report-tile {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px 16px;
  border: none;
  text-align: left;
  cursor: pointer;
  color: var(--text);
  transition: background .15s;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
}

.report-tile:active { background: #252540; }

.report-tile-name {
  font-size: 15px; font-weight: 600;
  margin-bottom: 3px;
}

.report-tile-desc {
  font-size: 12px; color: var(--text-muted);
}

/* === Bottom Nav === */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-header);
  display: flex;
  border-top: 1px solid var(--border);
  z-index: 50;
}

.nav-item {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 3px;
  background: none; border: none;
  color: var(--text-muted);
  font-size: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s;
}

.nav-item.active { color: var(--primary); }
.nav-item:active { color: var(--primary); }

/* === Report Controls === */
.report-controls {
  margin-bottom: 16px;
}

.period-picker {
  display: flex; gap: 8px; align-items: center;
  flex-wrap: wrap;
}

.period-input {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 14px;
  flex: 1;
  min-width: 0;
}

.period-input.short { max-width: 100px; flex: 0 0 auto; }

.period-picker span { color: var(--text-muted); }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary:active { opacity: 0.8; }

/* === Loading & Error === */
.loading {
  text-align: center;
  padding: 40px 0;
  color: var(--text-muted);
}

.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 0 auto 12px;
}

@keyframes spin { to { transform: rotate(360deg); } }

.error-msg {
  background: rgba(234,67,53,.1);
  color: var(--red);
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  margin: 12px 0;
}

/* === Data Tables === */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
  background: var(--bg-card);
  margin-bottom: 16px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  white-space: nowrap;
}

.data-table th,
.data-table td {
  padding: 8px 10px;
  text-align: right;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  position: sticky; top: 0;
  background: var(--bg-header);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.3px;
}

.data-table td:first-child,
.data-table th:first-child {
  text-align: left;
  position: sticky; left: 0;
  background: var(--bg-card);
  z-index: 1;
  min-width: 100px;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.data-table th:first-child {
  background: var(--bg-header);
  z-index: 2;
}

.data-table tr:last-child td { border-bottom: none; }

.data-table .total-row {
  font-weight: 700;
  background: rgba(66,133,244,.08);
}

.data-table .total-row td:first-child {
  background: rgba(66,133,244,.08);
}

.data-table .positive { color: var(--green); }
.data-table .negative { color: var(--red); }

/* === Chart Container === */
.chart-box {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 16px;
}

.chart-box canvas {
  width: 100% !important;
  max-height: 250px;
}

.chart-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 10px;
}

/* === Report Sections === */
.report-section {
  margin-bottom: 20px;
}

.report-section-title {
  font-size: 14px; font-weight: 600;
  margin-bottom: 10px;
  padding-left: 4px;
  color: var(--text);
}

/* === Calendar Grid === */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 16px;
}

.cal-header {
  font-size: 11px;
  text-align: center;
  color: var(--text-muted);
  padding: 4px 0;
  font-weight: 600;
}

.cal-day {
  background: var(--bg-card);
  border-radius: 8px;
  padding: 6px 2px;
  text-align: center;
  min-height: 52px;
}

.cal-day.empty { background: transparent; }

.cal-day-num {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.cal-day-val {
  font-size: 12px;
  font-weight: 600;
}

.cal-day.weekend { background: rgba(66,133,244,.08); }
.cal-day.today { border: 1px solid var(--primary); }

/* === Summary Cards === */
.summary-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.summary-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 12px;
}

.summary-card-value {
  font-size: 18px; font-weight: 700;
  margin-bottom: 2px;
}

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

.summary-card.wide {
  grid-column: 1 / -1;
}

/* === PNL / Waterfall === */
.pnl-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.pnl-row.header {
  font-weight: 700;
  background: rgba(66,133,244,.06);
}

.pnl-row.sub {
  padding-left: 28px;
  font-size: 13px;
  color: var(--text-muted);
}

.pnl-list {
  background: var(--bg-card);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 16px;
}

/* === Stock Table Extras === */
.stock-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
}

.stock-badge.low { background: rgba(234,67,53,.15); color: var(--red); }
.stock-badge.ok { background: rgba(52,168,83,.15); color: var(--green); }

/* === Pull-to-refresh indicator === */
.ptr-indicator {
  text-align: center;
  padding: 8px;
  color: var(--text-muted);
  font-size: 13px;
  display: none;
}

/* === Tabs in report === */
.report-tabs {
  display: flex; gap: 0;
  margin-bottom: 12px;
  background: var(--bg-card);
  border-radius: 8px;
  overflow: hidden;
}

.report-tab {
  flex: 1;
  padding: 10px 8px;
  border: none; background: none;
  color: var(--text-muted);
  font-size: 13px; font-weight: 600;
  cursor: pointer;
  transition: all .15s;
  -webkit-tap-highlight-color: transparent;
}

.report-tab.active {
  background: var(--primary);
  color: #fff;
}

/* === Misc === */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

.badge-blue { background: rgba(66,133,244,.15); color: var(--primary); }
.badge-green { background: rgba(52,168,83,.15); color: var(--green); }
.badge-red { background: rgba(234,67,53,.15); color: var(--red); }
.badge-orange { background: rgba(251,188,4,.15); color: var(--orange); }

/* === Responsive === */
@media (min-width: 600px) {
  .page { padding: 16px 24px; }
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  .summary-cards { grid-template-columns: repeat(4, 1fr); }
}
