:root {
  --bg-start: #f6f3ec;
  --bg-end: #e5efe8;
  --panel: rgba(255, 252, 246, 0.9);
  --ink: #1f2a20;
  --ink-soft: #4a554c;
  --accent: #0f7b6c;
  --accent-strong: #0a5f53;
  --danger: #b53939;
  --ok: #1f8f4e;
  --edge: #c7d0c6;
  --ring: rgba(15, 123, 108, 0.25);
  --shadow: 0 20px 50px rgba(21, 42, 27, 0.14);
  --radius: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Grotesk", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 10%, #fffaf0 0%, transparent 45%),
    radial-gradient(circle at 90% 85%, #d9f0e6 0%, transparent 40%),
    linear-gradient(145deg, var(--bg-start), var(--bg-end));
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px;
}

.app-shell {
  width: min(1300px, 98vw);
  background: var(--panel);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(199, 208, 198, 0.8);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: riseIn 380ms ease-out;
}

.hero {
  background: linear-gradient(120deg, #173d36, #1f5b51 65%, #2d7f70);
  color: #f7fff8;
  padding: 30px 28px;
}

.hero h1 {
  margin: 0;
  font-family: "Fraunces", serif;
  font-weight: 800;
  font-size: clamp(1.6rem, 3vw, 2.3rem);
  letter-spacing: 0.02em;
}

.hero p {
  margin: 10px 0 0;
  color: #d7f0e8;
  max-width: 68ch;
  line-height: 1.45;
}

.content {
  padding: 24px;
  display: grid;
  gap: 22px;
}

.tabs {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.tab-btn {
  background: #e7efea;
  border: 1px solid var(--edge);
  color: var(--ink);
}

.tab-btn.active {
  background: var(--accent);
  color: #f2fffa;
}

.panel {
  border: 1px solid var(--edge);
  border-radius: 14px;
  padding: 16px;
  background: #fffefb;
}

.panel.hidden {
  display: none;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 8px;
}

.table-wrap::-webkit-scrollbar {
  height: 8px;
  display: block !important;
}

.table-wrap::-webkit-scrollbar-track {
  background: rgba(15, 123, 108, 0.05);
  border-radius: 4px;
}

.table-wrap::-webkit-scrollbar-thumb {
  background: rgba(15, 123, 108, 0.3);
  border-radius: 4px;
}

.table-wrap::-webkit-scrollbar-thumb:hover {
  background: rgba(15, 123, 108, 0.5);
}

.panel-actions {
  margin-top: 12px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

th,
td {
  border-bottom: 1px solid #e1e8e2;
  text-align: left;
  padding: 14px 12px;
  font-size: 0.95rem;
}

th {
  color: #345146;
  font-weight: 700;
  background: #f4f8f4;
}

td {
  color: #27362f;
}

form {
  display: grid;
  gap: 18px;
}

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

.field {
  display: grid;
  gap: 8px;
  opacity: 0;
  animation: fadeUp 480ms ease forwards;
}

.field:nth-child(1) { animation-delay: 40ms; }
.field:nth-child(2) { animation-delay: 80ms; }
.field:nth-child(3) { animation-delay: 120ms; }
.field:nth-child(4) { animation-delay: 160ms; }
.field:nth-child(5) { animation-delay: 200ms; }
.field:nth-child(6) { animation-delay: 240ms; }
.field:nth-child(7) { animation-delay: 280ms; }
.field:nth-child(8) { animation-delay: 320ms; }
.field:nth-child(9) { animation-delay: 360ms; }

.span-2 {
  grid-column: span 2;
}

label {
  font-weight: 700;
  color: var(--ink);
  font-size: 0.95rem;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--edge);
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: #ffffff;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.02);
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  background: #ffffff;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--ring), inset 0 1px 2px rgba(0,0,0,0.02);
}

/* Remove default spin buttons for number inputs for cleaner layout */
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}
input[type=number] {
  -moz-appearance: textfield;
}

.help {
  color: var(--ink-soft);
  font-size: 0.85rem;
  margin-top: -2px;
}

.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 4px;
}

.switch input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

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

button {
  border: 0;
  border-radius: 12px;
  padding: 11px 18px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0,0,0,0.04);
  transition: transform 130ms ease, filter 130ms ease, box-shadow 130ms ease;
}

button:hover {
  transform: translateY(-1px);
  filter: brightness(1.03);
  box-shadow: 0 4px 8px rgba(0,0,0,0.08);
}

.primary {
  background: var(--accent);
  color: #f2fffa;
}

.secondary {
  background: #fdfdfd;
  color: var(--ink);
  border: 1px solid var(--edge);
}

button.small {
  font-size: 0.78rem;
  padding: 4px 10px;
}

.actions-cell {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: nowrap;
  white-space: nowrap;
}

.btn-edit {
  background: #e8f4fb;
  color: #1a6fa8;
  border: 1px solid #b3d9f0;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 130ms ease, transform 130ms ease;
}

.btn-edit:hover {
  background: #c5e5f7;
  transform: translateY(-1px);
}

.btn-delete {
  background: #fdeaea;
  color: #b53939;
  border: 1px solid #f0b3b3;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.88rem;
  font-weight: 900;
  cursor: pointer;
  line-height: 1;
  transition: background 130ms ease, transform 130ms ease;
}

.btn-delete:hover {
  background: #f7c5c5;
  transform: translateY(-1px);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 24, 18, 0.5);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 1000;
}

.modal.hidden {
  display: none;
}

.modal-card {
  width: min(680px, 100%);
  background: #ffffff;
  border: 1px solid rgba(0,0,0,0.05);
  border-radius: 20px;
  padding: 28px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.2);
  animation: modalIn 300ms cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-card--sm {
  width: min(420px, 100%);
}

@keyframes modalIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* Select + Add button inline layout */
.select-with-add {
  display: flex;
  gap: 8px;
  align-items: center;
}

.select-with-add select {
  flex: 1;
  min-width: 0;
}

.add-option-btn {
  flex-shrink: 0;
  background: var(--accent);
  color: #f2fffa;
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 130ms ease, filter 130ms ease;
}

.add-option-btn:hover {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

.summary-grid {
  display: grid;
  gap: 10px;
}

.compact-summary {
  margin-top: 14px;
}

.summary-row {
  display: grid;
  grid-template-columns: minmax(180px, 260px) minmax(120px, 1fr);
  gap: 10px;
  align-items: center;
  border: 1px solid #e1e8e2;
  border-radius: 12px;
  background: #f8fbf8;
  padding: 10px 12px;
}

.summary-label {
  font-weight: 700;
  color: #345146;
  word-break: break-word;
}

.summary-value {
  color: var(--ink);
  text-align: right;
  font-weight: 600;
}

.summary-empty {
  border: 1px dashed var(--edge);
  border-radius: 12px;
  padding: 16px;
  color: var(--ink-soft);
  background: #f7faf7;
}

.breakdown-stack {
  display: grid;
  gap: 16px;
  margin-top: 14px;
}

.breakdown-section {
  border: 1px solid #dbe4dc;
  border-radius: 14px;
  padding: 14px;
  background: linear-gradient(180deg, #fbfdfb, #f4f9f4);
}

.breakdown-section h3 {
  margin: 0 0 12px;
  font-size: 1rem;
  color: #26483d;
}

.breakdown-note {
  margin-top: 8px;
  color: var(--ink-soft);
  font-size: 0.88rem;
}

#status {
  min-height: 1.2em;
  font-weight: 600;
}

#status.ok {
  color: var(--ok);
}

#status.error {
  color: var(--danger);
}

@media (max-width: 760px) {
  /* ── Layout: edge-to-edge full screen ─── */
  body {
    padding: 0;
    align-items: flex-start;
  }

  .app-shell {
    width: 100vw;
    max-width: 100%;
    border-radius: 0;
    border: none;
    box-shadow: none;
    min-height: 100vh;
    overflow-x: hidden;
  }

  .hero {
    padding: 18px 16px 14px;
  }

  .hero h1 {
    font-size: 1.25rem;
  }

  .hero p {
    font-size: 0.88rem;
  }

  .content {
    padding: 14px 12px;
    gap: 16px;
  }

  /* ── Tabs: wrap into 2-column grid so all tabs are visible ─── */
  .tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    overflow-x: visible;
  }

  .tab-btn {
    white-space: nowrap;
    padding: 8px 12px;
    font-size: 0.82rem;
    border-radius: 8px;
  }

  /* ── Forms ─── */
  .grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .span-2 {
    grid-column: span 1;
  }

  .panel {
    padding: 12px;
  }

  .panel-head {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    margin-bottom: 12px;
  }

  .panel-head h2 {
    font-size: 1rem;
  }

  .panel-head button {
    width: 100%;
  }

  .actions button {
    width: 100%;
  }

  .summary-row {
    grid-template-columns: 1fr;
  }

  .summary-value {
    text-align: left;
  }

  /* ── Table → Card Transformation ─── */

  /* Critical: reset the 760px min-width that causes overflow */
  .table-wrap {
    overflow-x: visible;
  }

  .table-wrap table {
    display: block;
    width: 100% !important;
    min-width: 0 !important;
    border-collapse: separate;
    border-spacing: 0;
  }

  .table-wrap thead {
    display: none; /* hide header row completely */
  }

  .table-wrap tbody {
    display: block;
    width: 100%;
  }

  /* Each row becomes a card */
  .table-wrap tr {
    display: block;
    width: 100%;
    border: 1px solid var(--edge);
    border-radius: 12px;
    margin-bottom: 10px;
    padding: 6px 0;
    background: #fffefb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    overflow: hidden;
  }

  /* Each cell becomes a full-width row with label + value */
  .table-wrap td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    box-sizing: border-box;
    border: none;
    border-bottom: 1px solid rgba(199, 208, 198, 0.4);
    padding: 9px 14px;
    font-size: 0.88rem;
    min-height: 0;
    text-align: right;
    gap: 12px;
  }

  .table-wrap td:last-child {
    border-bottom: none;
    justify-content: center;
    flex-direction: column;
    padding: 10px 14px;
    gap: 6px;
  }

  /* Label shown before the value */
  .table-wrap td::before {
    content: attr(data-label);
    font-weight: 700;
    font-size: 0.78rem;
    color: var(--ink-soft);
    text-align: left;
    flex-shrink: 0;
    min-width: 40%;
    white-space: nowrap;
  }

  .table-wrap td:last-child::before {
    display: none;
  }

  /* Buttons in action cell — allow side-by-side on mobile too */
  .table-wrap td:last-child {
    border-bottom: none;
    justify-content: center;
    flex-direction: row;
    flex-wrap: wrap;
    padding: 10px 14px;
    gap: 8px;
  }

  .table-wrap td:last-child::before {
    display: none;
  }

  /* Buttons in action cell go full width only when alone */
  .table-wrap td:last-child button {
    flex: 1 1 auto;
    padding: 9px 12px;
  }
}

@keyframes riseIn {
  from {
    transform: translateY(12px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes fadeUp {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
