@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Space+Grotesk:wght@500;600;700&display=swap');

:root {
  /* Monochrome base */
  --bg: #FAFAFA;
  --surface: #FFFFFF;
  --text: #171717;
  --text-secondary: #737373;
  --text-tertiary: #A3A3A3;
  --border: #E5E5E5;
  --border-strong: #D4D4D4;
  --muted: #F2F2F2;
  --muted-hover: #ECECEC;

  /* Single accent color — used sparingly */
  --accent: #2563EB;
  --accent-hover: #1D4ED8;
  --accent-soft: rgba(37, 99, 235, 0.08);

  /* Functional colors */
  --success: #16A34A;
  --warning: #F59E0B;
  --danger: #DC2626;

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 1px 2px rgba(0, 0, 0, 0.02);
  --shadow-card: 0 4px 6px -1px rgba(0, 0, 0, 0.03), 0 2px 4px -2px rgba(0, 0, 0, 0.02);
  --shadow-elevated: 0 10px 15px -3px rgba(0, 0, 0, 0.05), 0 4px 6px -4px rgba(0, 0, 0, 0.02);

  --backdrop: rgba(0, 0, 0, 0.45);
}

[data-theme="dark"] {
  --bg: #0A0A0A;
  --surface: #141414;
  --text: #F5F5F5;
  --text-secondary: #A3A3A3;
  --text-tertiary: #737373;
  --border: #262626;
  --border-strong: #404040;
  --muted: #1A1A1A;
  --muted-hover: #262626;

  --accent: #3B82F6;
  --accent-hover: #60A5FA;
  --accent-soft: rgba(59, 130, 246, 0.12);

  --success: #22C55E;
  --warning: #FBBF24;
  --danger: #EF4444;

  --shadow: 0 1px 3px rgba(0, 0, 0, 0.25), 0 1px 2px rgba(0, 0, 0, 0.15);
  --shadow-card: 0 4px 6px -1px rgba(0, 0, 0, 0.35), 0 2px 4px -2px rgba(0, 0, 0, 0.25);
  --shadow-elevated: 0 10px 15px -3px rgba(0, 0, 0, 0.45), 0 4px 6px -4px rgba(0, 0, 0, 0.35);

  --backdrop: rgba(0, 0, 0, 0.65);
}

* { box-sizing: border-box; }

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

h1, h2, h3, h4, h5, h6 {
  font-family: 'Space Grotesk', 'Inter', sans-serif;
  letter-spacing: -0.02em;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 16px;
}

/* Header */
.header {
  padding: 12px 0 24px;
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-icon {
  width: 44px;
  height: 44px;
  background: var(--text);
  color: var(--surface);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.brand-icon svg {
  width: 26px;
  height: 26px;
}
[data-theme="dark"] .brand-icon {
  border: 1px solid var(--border-strong);
}
.brand h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 700;
}
.subtitle {
  margin: 0;
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.month-selector {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
}
.month-selector select {
  border: none;
  background: transparent;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  padding: 2px 4px;
  outline: none;
  font-family: 'Space Grotesk', sans-serif;
}

/* Buttons */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  font-family: inherit;
}
.btn:hover {
  background: var(--muted);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}
.btn:active {
  transform: scale(0.98);
}
.btn.primary {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}
.btn.primary:hover {
  background: #333;
  border-color: #333;
}
.btn.accent {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn.accent:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
}
.btn.ghost:hover {
  background: var(--muted);
}
.btn.block { display: block; width: 100%; }

/* Status */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 14px;
  font-weight: 500;
}
.live-dot {
  color: var(--success);
  font-size: 10px;
  margin-right: 4px;
}

/* Envelopes Grid */
.envelopes-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 18px;
}
@media (min-width: 720px) {
  .envelopes-grid { grid-template-columns: repeat(2, 1fr); }
}

/* Envelope Card */
.envelope-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow-card);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, border-color 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  overflow: hidden;
}
.envelope-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-elevated);
  border-color: var(--border-strong);
}
.envelope-card:active { transform: scale(0.995); }

.envelope-card.warning { border-color: var(--warning); }
.envelope-card.critical {
  border-color: var(--danger);
  animation: critical-pulse 1.4s infinite ease-in-out;
}
.envelope-card.over {
  border-color: var(--danger);
  animation: shake 0.55s infinite ease-in-out;
}

@keyframes critical-pulse {
  0%, 100% { box-shadow: 0 0 0 1px rgba(220, 38, 38, 0.15); }
  50% { box-shadow: 0 0 0 4px rgba(220, 38, 38, 0.08); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-2px); }
  40%, 80% { transform: translateX(2px); }
}

.env-row {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 10px;
  align-items: center;
}
.env-col {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.env-col.center { text-align: center; }
.env-col.right { text-align: right; }

.env-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  background: var(--muted);
  border: 1px solid var(--border);
}

.env-name-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}

.env-name {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
  font-family: 'Space Grotesk', sans-serif;
}
.env-budget {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.col-label {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}
.col-value {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  font-family: 'Space Grotesk', sans-serif;
}
.col-value.red { color: var(--danger); }
.col-value.yellow { color: var(--warning); }
.col-value.green { color: var(--success); }

/* Horizontal bar */
.env-bar-track {
  position: relative;
  height: 8px;
  background: var(--muted);
  border-radius: 999px;
  overflow: hidden;
}
.env-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.5s cubic-bezier(0.2, 0.9, 0.3, 1), background 0.3s ease;
}

.env-bar-fill.green { background: var(--success); }
.env-bar-fill.yellow { background: var(--warning); }
.env-bar-fill.red { background: var(--danger); }

.env-bar-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 5px;
}
.env-bar-pct {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  font-family: 'Space Grotesk', sans-serif;
}
.env-bar-pct.red { color: var(--danger); }
.env-bar-pct.yellow { color: var(--warning); }
.env-bar-pct.green { color: var(--success); }

/* Total Card */
.total-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-card);
  border: 1px solid var(--border);
}
.total-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
}
.total-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 600;
}
.total-value {
  font-size: 22px;
  font-weight: 700;
  font-family: 'Space Grotesk', sans-serif;
}
.total-spent { text-align: right; }
.spent-value { font-size: 18px; font-weight: 700; font-family: 'Space Grotesk', sans-serif; }
.spent-percent { font-size: 13px; color: var(--text-secondary); font-weight: 600; }

.total-tube {
  position: relative;
  height: 16px;
  background: var(--muted);
  border-radius: 999px;
  overflow: hidden;
  margin: 6px 0 10px;
  display: flex;
}
.total-segment {
  height: 100%;
  position: relative;
  transition: width 0.5s cubic-bezier(0.2, 0.9, 0.3, 1);
  opacity: 0.9;
}
.total-segment.spent {
  opacity: 0.35;
  filter: grayscale(1);
}
.total-segment + .total-segment {
  border-left: 1px solid var(--surface);
}

.total-tube::after {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  left: var(--spent-boundary, 0%);
  width: 2px;
  background: var(--text);
  opacity: 0.3;
  transition: left 0.5s cubic-bezier(0.2, 0.9, 0.3, 1);
  z-index: 2;
}

.total-remaining {
  font-size: 13px;
  color: var(--text-secondary);
}

/* Quick actions */
.quick-actions {
  display: flex;
  gap: 10px;
  margin: 18px 0 8px;
}
.quick-actions .btn { flex: 1; }

/* Footer */
.footer-hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 14px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
}
@media (min-width: 620px) {
  .modal { align-items: center; }
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--backdrop);
  backdrop-filter: blur(2px);
}
.modal-content {
  position: relative;
  background: var(--surface);
  width: 100%;
  max-height: 92vh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow-elevated);
  overflow: hidden;
  z-index: 2;
}
@media (min-width: 620px) {
  .modal-content {
    width: 460px;
    margin: 0 auto;
    border-radius: var(--radius);
    max-height: 86vh;
  }
}
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--border);
}
.modal-title {
  font-size: 18px;
  font-weight: 700;
}
.modal-sub {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}
.modal-close {
  background: var(--muted);
  border: none;
  font-size: 20px;
  line-height: 1;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease;
}
.modal-close:hover { background: var(--muted-hover); }
.modal-body { padding: 16px 18px 22px; }

.modal-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 18px;
}
.stat {
  background: var(--muted);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.stat-label { font-size: 11px; color: var(--text-secondary); font-weight: 600; }
.stat-value { font-size: 17px; font-weight: 700; margin: 2px 0; font-family: 'Space Grotesk', sans-serif; }
.stat-percent { font-size: 12px; color: var(--text-secondary); }

/* Transactions */
.tx-section { margin-bottom: 18px; }
.tx-header {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.tx-list {
  max-height: 148px;
  overflow-y: auto;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.tx-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.tx-item:last-child { border-bottom: none; }
.tx-item .tx-note { color: var(--text-secondary); font-size: 12px; }
.tx-item .tx-amt { font-weight: 600; white-space: nowrap; }

/* Form */
.add-form { margin-top: 6px; }
.form-row { display: flex; gap: 10px; margin-bottom: 10px; }
.form-field { flex: 1; }
.form-field.flex-2 { flex: 2; }
.form-field label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.form-field input,
.form-field select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  font-family: inherit;
}
.form-field input:focus,
.form-field select:focus {
  outline: none;
  border-color: var(--text);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.warning-text {
  margin-top: 8px;
  font-size: 12px;
  color: var(--danger);
  font-weight: 600;
}

/* Settings */
.settings-content { max-width: 520px; }
.settings-body { padding: 4px 18px 22px; }
.settings-section { margin: 18px 0; }
.section-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.settings-env-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.settings-env-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--muted);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 14px;
}
.env-info { display: flex; align-items: center; gap: 8px; }
.env-color-dot {
  width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0;
}
.env-actions { display: flex; gap: 6px; }
.env-actions button {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 6px;
}

.color-palette {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.color-swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform .1s;
}
.color-swatch.selected {
  border-color: var(--text);
  transform: scale(1.1);
}

.bot-config-help {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 12px;
  line-height: 1.4;
}
.bot-config-status {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 10px 0 14px;
  padding: 10px 12px;
  background: var(--muted);
  border-radius: var(--radius-sm);
}
.bot-config-status.connected {
  color: var(--success);
  background: rgba(22, 163, 74, 0.08);
}
.bot-config-status.error {
  color: var(--danger);
  background: rgba(220, 38, 38, 0.08);
}

.settings-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

/* Income modal */
#income-modal .modal-content { max-width: 420px; }

/* Readonly state */
.readonly-badge {
  font-size: 10px;
  background: var(--muted);
  padding: 1px 6px;
  border-radius: 999px;
  color: var(--text-secondary);
  margin-left: 6px;
}

/* Accent highlights */
.accent-text { color: var(--accent); }
.accent-bg { background: var(--accent-soft); }
