/* ============================================================
   STACK — Personal Budget
   Aesthetic: Editorial utility. Warm blacks, amber data, tight grid.
   ============================================================ */

/* Fonts loaded via <link> in index.html for faster rendering */

:root {
  --bg: #141414;
  --card: #1a1a1a;
  --surface: #1e1e1e;
  --surface-up: #252525;
  --border: #2e2e2e;
  --border-light: #383838;

  --text: #d4d0c8;
  --text-secondary: #8a8780;
  --text-dim: #5c5a55;

  --amber: #c8a44e;
  --amber-dim: #a68a3a;
  --amber-glow: rgba(200, 164, 78, 0.08);
  --green: #5a9a6a;
  --green-text: #7bc48e;
  --red: #b05050;
  --red-text: #d47272;
  --yellow: #b89940;

  --font: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --mono: 'JetBrains Mono', 'SF Mono', 'Fira Code', monospace;
  --header-height: 68px;
  --radius: 4px;
  --radius-md: 6px;
}

*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html { scroll-behavior: smooth; }

body {
  color-scheme: dark;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  margin: 0;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + var(--header-height) + 20px) 16px;
  padding-top: calc(env(safe-area-inset-top, 0px) + 16px);
  background: var(--bg);
  color: var(--text);
  -webkit-overflow-scrolling: touch;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Semantic colors */
.asset { color: var(--green-text); }
.liability { color: var(--red-text); }
.positive-amount { color: var(--green-text); }

/* ---- Layout ---- */

.container {
  margin: 0 auto;
  padding: 0;
  max-width: 1100px;
}

.top { display: none; }
.balances { display: none; }

/* ---- Grid ---- */

.lists {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

@media (min-width: 768px) {
  .lists { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 1024px) {
  .lists { grid-template-columns: repeat(4, 1fr); }
  body { padding: 24px 24px calc(env(safe-area-inset-bottom) + var(--header-height) + 24px) 24px; }
}

/* ---- Section Cards ---- */

.list {
  background: var(--card);
  padding: 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  box-shadow: none;
}

.list h2 {
  margin: 0 0 14px 0;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.section-total {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-left: auto;
  margin-right: 10px;
  letter-spacing: -0.02em;
}

.list h2 .addItemSectionBtn {
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text-dim);
  width: 26px;
  height: 26px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 15px;
  font-weight: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s;
  flex-shrink: 0;
  line-height: 1;
}

.list h2 .addItemSectionBtn:hover {
  color: var(--amber);
  border-color: var(--amber-dim);
}

.list h2 .addItemSectionBtn:active {
  transform: none;
  background: var(--amber-glow);
  color: var(--amber);
  border-color: var(--amber-dim);
}

/* ---- Footer Last Action ---- */

.footer-last-action {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 55vw;
  margin-top: 2px;
  letter-spacing: -0.01em;
}

.history-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.history-list li {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-secondary);
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  letter-spacing: -0.01em;
}

.history-list li:last-child {
  border-bottom: none;
}

/* ---- Item Cards ---- */

.item {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 6px;
  transition: background 0.1s;
  position: relative;
  overflow: hidden;
  border: 1px solid transparent;
}

.item:hover {
  border-color: var(--border);
}

.item .item-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  cursor: pointer;
}

.item:active { }

.item .item-info {
  display: flex;
  flex-wrap: nowrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.item .item-name {
  font-weight: 500;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.item .item-amount {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  letter-spacing: -0.03em;
}

.item .item-meta-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
}

.item .meta {
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.3;
  font-family: var(--mono);
  letter-spacing: -0.01em;
}

/* ---- Inline Actions ---- */

.item .item-actions-inline {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.icon-action-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  padding: 0;
}

.icon-action-btn:hover {
  border-color: var(--amber-dim);
  color: var(--amber);
}

.icon-action-btn:active {
  transform: none;
  background: var(--amber-glow);
  border-color: var(--amber);
  color: var(--amber);
}

.icon-action-btn.edit {
  color: var(--text-dim);
}

/* ---- Progress Bar ---- */

.item-progress {
  width: 100%;
  height: 2px;
  background: var(--border);
  border-radius: 1px;
  overflow: hidden;
  margin-top: 4px;
}

.item-progress-bar {
  height: 100%;
  border-radius: 1px;
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.item-progress-bar.good { background: var(--green); }
.item-progress-bar.warning { background: var(--yellow); }
.item-progress-bar.danger { background: var(--red); }

/* ---- Modal ---- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 9999;
  padding: 0;
  padding-top: env(safe-area-inset-top);
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideDown {
  from { transform: translateY(-20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal {
  background: var(--card);
  padding: 20px;
  padding-top: calc(20px + env(safe-area-inset-top));
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  border: 1px solid var(--border);
  border-top: none;
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  overflow-y: auto;
  color: inherit;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: slideDown 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

@media (min-width: 600px) {
  .modal-overlay {
    align-items: center;
    padding: 20px;
    padding-top: 20px;
  }
  .modal {
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    padding-top: 20px;
    padding-bottom: 20px;
    max-height: calc(100vh - 40px);
    animation: slideUp 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  }
  @keyframes slideUp {
    from { transform: translateY(12px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }
}

.modal h3 {
  margin: 0 0 18px 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.modal label {
  display: block;
  margin-bottom: 14px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

.modal label.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  color: var(--text);
  text-transform: none;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: normal;
}

.modal label.toggle-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--amber);
}

.modal input[type="text"],
.modal input[type="number"],
.modal input[type="date"],
.modal select {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: inherit;
  font-size: 15px;
  font-family: var(--font);
  margin-top: 6px;
  transition: border-color 0.15s;
}

.modal input[type="number"],
.modal input[type="date"] {
  font-family: var(--mono);
  font-size: 14px;
  letter-spacing: -0.02em;
}

.modal input:focus,
.modal select:focus {
  outline: none;
  border-color: var(--amber-dim);
  box-shadow: 0 0 0 1px var(--amber-glow);
}

.modal .actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.modal .actions button {
  padding: 10px 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  font-family: var(--font);
  min-height: 42px;
  transition: all 0.1s;
  background: var(--surface);
  color: var(--text);
}

.modal .actions button:active {
  transform: none;
  opacity: 0.8;
}

.modal .actions button#_item_ok,
.modal .actions button#_spend_ok {
  background: var(--amber);
  border-color: var(--amber);
  color: #141414;
  flex: 1;
}

.modal .actions button#_item_ok:hover,
.modal .actions button#_spend_ok:hover {
  background: var(--amber-dim);
  border-color: var(--amber-dim);
}

.modal .actions button#_item_cancel,
.modal .actions button#_spend_cancel {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border);
}

.modal .actions button.delBtn {
  background: transparent;
  border-color: var(--red);
  color: var(--red-text);
}

.modal .actions button.delBtn:hover {
  background: rgba(176, 80, 80, 0.1);
}

/* ---- Gist Controls ---- */

.gist-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.gist-controls input {
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: inherit;
  font-size: 14px;
  font-family: var(--mono);
}

.gist-controls input:focus {
  outline: none;
  border-color: var(--amber-dim);
}

.gist-buttons {
  display: flex;
  gap: 8px;
}

.gist-buttons button {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius);
  border: 1px solid var(--amber-dim);
  background: var(--amber-glow);
  color: var(--amber);
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  font-family: var(--font);
  min-height: 42px;
  transition: all 0.15s;
}

.gist-buttons button:hover {
  background: rgba(200, 164, 78, 0.15);
}

.gist-buttons button:active {
  transform: none;
  opacity: 0.8;
}

.status {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  padding: 6px;
}

/* ---- Backup Section ---- */

.settings-section {
  margin: 16px 0;
  padding: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.settings-section h4 {
  margin: 0 0 12px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--amber);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.section-visibility-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.section-visibility-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text-secondary);
}

.section-visibility-list label:hover {
  color: var(--text);
}

.section-visibility-list input {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.backup-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.backup-section h4 {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

.backup-buttons {
  display: flex;
  gap: 8px;
}

.backup-btn {
  flex: 1;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  font-family: var(--font);
  min-height: 40px;
  transition: all 0.15s;
}

.backup-btn:hover {
  border-color: var(--border-light);
  color: var(--text);
}

.backup-btn:active {
  transform: none;
  opacity: 0.8;
}

.backup-section small {
  color: var(--text-dim);
  font-size: 12px;
  text-align: center;
}

/* ---- Install App Section ---- */

.install-app-section {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.install-app-section small {
  color: var(--text-dim);
  font-size: 12px;
  text-align: center;
}


/* ---- Footer Bar ---- */

.footer-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)) 16px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  background: var(--card);
  border-top: 1px solid var(--border);
  -webkit-user-select: none;
  user-select: none;
  box-shadow: none;
}

.footer-left {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.available-amount {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.available-amount #available {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 24px;
  color: var(--amber);
  line-height: 1.1;
  letter-spacing: -0.03em;
}

.available-label {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 600;
}

.footer-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 16px;
  border-radius: var(--radius);
  padding: 10px;
  min-width: 42px;
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  cursor: pointer;
}

.icon-btn:hover {
  border-color: var(--border-light);
  color: var(--text);
}

.icon-btn:active {
  transform: none;
  background: var(--surface-up);
}

/* ---- Checkbox ---- */

.modal input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  accent-color: var(--amber);
}

/* ---- Spend History ---- */

.modal h4 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 18px 0 8px 0;
  color: var(--text-dim);
}

.modal ul {
  margin: 0;
  padding: 0;
  font-size: 13px;
  color: var(--text-secondary);
  list-style: none;
}

.modal ul.spend-history-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  margin-bottom: 4px;
  font-family: var(--mono);
  font-size: 12px;
}

.modal ul.spend-history-list li:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.modal ul.spend-history-list .spend-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal ul.spend-history-list .delete-spend-btn {
  background: transparent;
  border: none;
  color: var(--red-text);
  cursor: pointer;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: var(--radius);
  transition: background 0.15s;
  flex-shrink: 0;
}

.modal ul.spend-history-list .delete-spend-btn:hover {
  background: rgba(176, 80, 80, 0.12);
}

.modal ul.spend-history-list .delete-spend-btn:active {
  background: rgba(176, 80, 80, 0.2);
}

.modal small {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.5;
  margin-top: 12px;
}

/* ---- Empty State ---- */

.list-items:empty::after {
  content: '\2014'; /* em dash */
  display: block;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  padding: 16px;
  opacity: 0.4;
}

/* ---- Touch Feedback ---- */

@media (hover: none) {
  .item:active {
    background: var(--surface-up);
  }
}

/* ---- Scrollbar ---- */

.list-items {
  -webkit-overflow-scrolling: touch;
}

::-webkit-scrollbar {
  width: 0;
  height: 0;
}

* {
  scrollbar-width: none;
}

/* ---- Install Instructions ---- */

.install-instructions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 16px 0;
}

.install-method {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  border: 1px solid var(--border);
}

.install-method h4 {
  margin: 0 0 8px 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--amber);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.install-method ol {
  margin: 0;
  padding-left: 18px;
}

.install-method li {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  line-height: 1.4;
}

.install-method li:last-child {
  margin-bottom: 0;
}

.install-method li strong {
  color: var(--text);
}

.install-note {
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  margin: 10px 0 0 0;
  padding: 10px;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

/* ---- Autofill Modal ---- */

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

.autofill-freq-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
}

.autofill-freq-row select {
  flex: 1;
}

.autofill-header {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.autofill-list {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.autofill-section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--amber);
  padding: 6px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.autofill-item {
  display: flex;
  align-items: center;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  gap: 8px;
  cursor: pointer;
  transition: background 0.1s;
  -webkit-user-select: none;
  user-select: none;
}

.autofill-item:last-child {
  border-bottom: none;
}

.autofill-item:hover {
  background: var(--surface-up);
}

/* Dim content when unchecked (but keep unaffordable amount at full red) */
.autofill-item:has(.autofill-cb:not(:checked)) .autofill-rec-name,
.autofill-item:has(.autofill-cb:not(:checked)) .autofill-rec-meta,
.autofill-item:has(.autofill-cb:not(:checked)) .autofill-item-amount:not(.autofill-item-amount--unaffordable) {
  opacity: 0.35;
}

.autofill-item .autofill-cb {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  accent-color: var(--amber);
  cursor: pointer;
}

.autofill-rec-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.autofill-rec-meta {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
  letter-spacing: -0.01em;
}

.autofill-item-amount {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  color: var(--green-text);
  white-space: nowrap;
  flex-shrink: 0;
}

.autofill-item-amount--unaffordable {
  color: var(--red-text);
}

.autofill-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 0 0;
  margin-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.autofill-total-amount {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  color: var(--amber);
  letter-spacing: -0.02em;
}

.autofill-remaining {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0 0 0;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
}

.autofill-remaining span:last-child {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.autofill-hint {
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  padding: 20px 0 8px;
  margin: 0;
}

.modal .actions button#_af_fill {
  background: var(--amber);
  border-color: var(--amber);
  color: #141414;
  flex: 1;
}

.modal .actions button#_af_fill:hover:not(:disabled) {
  background: var(--amber-dim);
  border-color: var(--amber-dim);
}

.modal .actions button#_af_fill:disabled {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text-dim);
  cursor: not-allowed;
  opacity: 0.5;
}

.modal .actions button#_af_cancel {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border);
}
