:root {
  color-scheme: light dark;
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-muted: #f0eee8;
  --text: #151718;
  --muted: #6d7276;
  --line: rgba(22, 23, 24, 0.1);
  --income: #15803d;
  --income-soft: #dcfce7;
  --expense: #c2410c;
  --expense-soft: #ffedd5;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --gold: #b7791f;
  --danger: #b42318;
  --shadow: 0 18px 44px rgba(38, 32, 22, 0.08);
  --radius: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Helvetica Neue", Arial, sans-serif;
}

[data-theme="dark"] {
  --bg: #111315;
  --surface: #1a1d1f;
  --surface-muted: #23272a;
  --text: #f7f2e9;
  --muted: #b7b0a4;
  --line: rgba(247, 242, 233, 0.12);
  --income: #4ade80;
  --income-soft: rgba(74, 222, 128, 0.15);
  --expense: #fb923c;
  --expense-soft: rgba(251, 146, 60, 0.16);
  --accent: #5eead4;
  --accent-strong: #2dd4bf;
  --gold: #facc15;
  --danger: #f87171;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111315;
    --surface: #1a1d1f;
    --surface-muted: #23272a;
    --text: #f7f2e9;
    --muted: #b7b0a4;
    --line: rgba(247, 242, 233, 0.12);
    --income: #4ade80;
    --income-soft: rgba(74, 222, 128, 0.15);
    --expense: #fb923c;
    --expense-soft: rgba(251, 146, 60, 0.16);
    --accent: #5eead4;
    --accent-strong: #2dd4bf;
    --gold: #facc15;
    --danger: #f87171;
    --shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
  }
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100%;
  margin: 0;
  background:
    linear-gradient(180deg, rgba(15, 118, 110, 0.1), transparent 280px),
    var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
select {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

svg {
  display: block;
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.app-shell {
  width: min(100%, 1080px);
  margin: 0 auto;
  padding: calc(16px + var(--safe-top)) 16px calc(28px + var(--safe-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 0 18px;
}

.topbar > div:first-child {
  min-width: 0;
}

.topbar h1,
.section-heading h2,
.cycle-strip h2 {
  margin: 0;
  letter-spacing: 0;
}

.topbar h1 {
  font-size: clamp(2rem, 8vw, 4rem);
  line-height: 0.95;
  font-weight: 780;
  overflow-wrap: anywhere;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}

.toolbar,
.backup-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tool-button,
.segment,
.primary-button,
.secondary-button,
.text-button {
  border: 0;
  cursor: pointer;
  color: var(--text);
}

.tool-button {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.06);
}

.text-tool {
  font-weight: 800;
}

main {
  display: grid;
  gap: 14px;
}

.cycle-strip,
.panel,
.summary-primary,
.metric {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  box-shadow: var(--shadow);
}

.cycle-strip {
  display: grid;
  grid-template-columns: 42px 1fr 42px;
  align-items: center;
  gap: 10px;
  padding: 10px;
}

.cycle-strip div {
  min-width: 0;
  text-align: center;
}

.cycle-strip h2 {
  font-size: 0.95rem;
}

.cycle-strip p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.summary-primary {
  grid-column: 1 / -1;
  padding: 18px;
  background:
    linear-gradient(135deg, rgba(15, 118, 110, 0.14), transparent 54%),
    var(--surface);
}

.summary-row,
.section-heading,
.transaction-meta,
.transaction-main,
.transaction-amount {
  display: flex;
  align-items: center;
}

.summary-row,
.section-heading {
  justify-content: space-between;
  gap: 12px;
}

.summary-row p,
.metric p {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.summary-row span {
  color: var(--accent);
  font-size: 0.88rem;
  font-weight: 800;
}

.summary-primary strong {
  display: block;
  margin: 8px 0 14px;
  font-size: clamp(1.85rem, 8.4vw, 4.2rem);
  line-height: 0.98;
  overflow-wrap: anywhere;
}

.progress-track {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-muted);
}

.progress-fill {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), var(--gold), var(--expense));
  transition: width 180ms ease;
}

.metric {
  padding: 14px;
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: clamp(1.15rem, 5vw, 1.7rem);
}

.metric-income {
  background: linear-gradient(180deg, var(--income-soft), var(--surface));
}

.metric-expense {
  background: linear-gradient(180deg, var(--expense-soft), var(--surface));
}

.panel {
  padding: 16px;
}

.section-heading {
  margin-bottom: 14px;
}

.section-heading h2 {
  font-size: 1.05rem;
  font-weight: 780;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.segment {
  min-height: 42px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.segment.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 7px 20px rgba(0, 0, 0, 0.08);
}

.form-grid,
.settings-grid {
  display: grid;
  gap: 12px;
}

.form-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin: 14px 0;
}

label {
  display: grid;
  gap: 7px;
  min-width: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

input,
select {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0 12px;
  outline: none;
}

input[type="date"] {
  display: block;
  -webkit-appearance: none;
  appearance: none;
  height: 46px;
  line-height: 46px;
  padding-top: 0;
  padding-bottom: 0;
  text-align: center;
}

input[type="date"]::-webkit-date-and-time-value {
  width: 100%;
  min-width: 0;
  margin: 0;
  line-height: 46px;
  text-align: center;
}

input[type="date"]::-webkit-calendar-picker-indicator {
  flex: 0 0 auto;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
}

.primary-button,
.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  border-radius: var(--radius);
  font-weight: 850;
}

.primary-button {
  width: 100%;
  background: var(--text);
  color: var(--surface);
}

.secondary-button {
  min-width: 120px;
  border: 1px solid var(--line);
  background: var(--surface-muted);
}

.text-button {
  min-height: 34px;
  border-radius: var(--radius);
  background: var(--surface-muted);
  padding: 0 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.analytics-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
}

.chart-wrap {
  display: grid;
  min-height: 230px;
  place-items: center;
}

.chart-wrap.wide {
  min-height: 220px;
}

canvas {
  width: 100%;
  max-width: 320px;
  height: auto;
}

#categoryChart {
  max-width: 220px;
}

.legend {
  display: grid;
  gap: 8px;
}

.legend-row {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.86rem;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.legend-row strong {
  color: var(--text);
  font-size: 0.86rem;
}

.settings-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.backup-actions {
  grid-column: 1 / -1;
  flex-wrap: wrap;
}

.transaction-list {
  display: grid;
  gap: 10px;
}

.transaction-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  min-height: 70px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.transaction-main {
  min-width: 0;
  gap: 10px;
}

.category-mark {
  flex: 0 0 11px;
  width: 11px;
  height: 42px;
  border-radius: 999px;
}

.transaction-text {
  min-width: 0;
}

.transaction-text strong,
.transaction-text span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.transaction-text strong {
  font-size: 0.95rem;
}

.transaction-text span,
.transaction-meta {
  color: var(--muted);
  font-size: 0.78rem;
}

.transaction-meta {
  gap: 6px;
  margin-top: 3px;
}

.transaction-amount {
  justify-content: flex-end;
  gap: 8px;
  color: var(--text);
  font-weight: 850;
  white-space: nowrap;
}

.transaction-amount.income {
  color: var(--income);
}

.transaction-amount.expense {
  color: var(--expense);
}

.delete-button {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 0;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  cursor: pointer;
}

.empty-state {
  min-height: 92px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  text-align: center;
  padding: 18px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + var(--safe-bottom));
  z-index: 20;
  max-width: min(92vw, 440px);
  transform: translate(-50%, 18px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--surface);
  opacity: 0;
  pointer-events: none;
  padding: 12px 14px;
  font-weight: 800;
  box-shadow: var(--shadow);
  transition:
    opacity 160ms ease,
    transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

[dir="rtl"] body {
  font-family:
    "Geeza Pro", "Noto Naskh Arabic", "Tahoma", Arial, ui-sans-serif, system-ui,
    sans-serif;
}

[dir="rtl"] .transaction-card,
[dir="rtl"] .legend-row {
  direction: rtl;
}

[dir="rtl"] .transaction-amount {
  justify-content: flex-start;
}

[dir="rtl"] .cycle-strip svg {
  transform: scaleX(-1);
}

@media (min-width: 760px) {
  .app-shell {
    padding-right: 28px;
    padding-left: 28px;
  }

  main {
    grid-template-columns: repeat(12, minmax(0, 1fr));
  }

  .cycle-strip,
  .summary-grid,
  .add-panel,
  .transactions-panel {
    grid-column: span 7;
  }

  .analytics-grid,
  .settings-panel {
    grid-column: span 5;
  }

  .summary-grid {
    display: grid;
  }

  .analytics-grid {
    align-self: start;
  }

  .transactions-panel {
    order: 10;
  }
}

@media (min-width: 980px) {
  .analytics-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .form-grid,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .backup-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .secondary-button {
    min-width: 0;
  }

  .transaction-card {
    grid-template-columns: 1fr;
  }

  .transaction-amount {
    justify-content: space-between;
  }
}

@media (display-mode: standalone) and (max-width: 480px) {
  .app-shell {
    padding-top: max(calc(16px + var(--safe-top)), 56px);
  }
}
