:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --line: #e2e5ea;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #2457c5;
  --accent-ink: #ffffff;
  --danger: #b42318;
  --ok: #157347;
  --radius: 8px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.top {
  display: flex;
  align-items: center;
  gap: 24px;
  height: 52px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { font-weight: 600; font-size: 16px; }

.top nav { display: flex; gap: 4px; }

.top nav a {
  padding: 6px 12px;
  border-radius: var(--radius);
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
}

.top nav a:hover { color: var(--text); background: var(--bg); }
.top nav a.active { color: var(--accent); background: #e8eefb; }

.layout {
  display: flex;
  height: calc(100% - 52px);
}

.content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 20px;
  gap: 12px;
}

.toolbar { display: flex; align-items: center; gap: 12px; }

.toolbar h1 { margin: 0 auto 0 0; font-size: 20px; font-weight: 600; }

input, textarea, select {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

input:focus, textarea:focus, select:focus { outline: 2px solid #b9cbf3; border-color: var(--accent); }

#search { width: 240px; }

textarea { min-height: 120px; resize: vertical; }

button, .button {
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}

button:hover, .button:hover { background: var(--bg); }

button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { background: #1d49a6; }
button.danger { color: var(--danger); border-color: #f0c4bf; }
button.danger:hover { background: #fdf1f0; }
button.icon { padding: 2px 10px; font-size: 20px; line-height: 1; border: 0; background: none; color: var(--muted); }
button.icon:hover { color: var(--text); }

.totals { display: flex; gap: 12px; }
.totals:empty { display: none; }

.totals .card {
  flex: 1;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.totals .label { color: var(--muted); font-size: 12px; }
.totals .value { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.totals .neg { color: var(--danger); }
.totals .pos { color: var(--ok); }

.grid-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.grid { width: 100%; border-collapse: collapse; }

.grid th, .grid td {
  padding: 9px 14px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 420px;
}

.grid th {
  position: sticky;
  top: 0;
  background: var(--bg);
  color: var(--muted);
  font-weight: 500;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .03em;
  cursor: pointer;
  user-select: none;
}

.grid th.sorted::after { content: " ↓"; }
.grid th.sorted.asc::after { content: " ↑"; }

.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover { background: #f8f9fb; }
.grid tbody tr.selected { background: #e8eefb; }
.grid tr.done td { color: var(--muted); text-decoration: line-through; }

.grid .num { text-align: right; font-variant-numeric: tabular-nums; }

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--bg);
  color: var(--muted);
}

.tag.paid { background: #e3f4ea; color: var(--ok); }

.footnote { margin: 0; padding: 14px; color: var(--muted); }

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(31, 36, 48, .45);
  animation: fade .15s ease-out;
}

.overlay[hidden] { display: none; }

.modal {
  width: 100%;
  max-width: 480px;
  max-height: calc(100% - 40px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
  animation: rise .18s ease-out;
}

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.panel-body { flex: 1; overflow: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 14px; }

.field label, .field .label { display: block; margin-bottom: 4px; color: var(--muted); font-size: 12px; }

.field .readonly { padding: 7px 0; }

.field.check { display: flex; align-items: center; gap: 8px; }
.field.check input { width: auto; }
.field.check label { margin: 0; color: var(--text); font-size: 14px; }

.row { display: flex; gap: 12px; }
.row .field { flex: 1; }

.error { margin: 0; padding: 10px 20px; color: var(--danger); }

.panel-actions {
  display: flex;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--line);
}

.panel-actions .danger { margin-left: auto; }

@media (max-width: 900px) {
  .layout { height: auto; }
  .totals { flex-wrap: wrap; }
  .content { padding: 16px; }
}
