:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --text: #0a0a0a;
  --muted: #737373;
  --border: rgba(0, 0, 0, 0.1);
  --accent: #0a0a0a;
  --accent-text: #ffffff;
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
}
@supports (font-variation-settings: normal) {
  :root { font-family: InterVariable, ui-sans-serif, system-ui, sans-serif; }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

/* Login */
.login {
  min-height: 100vh;
  min-height: 100dvh;
  padding: 20px;
  display: grid;
  place-items: center;
}
.login-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(320px, 90vw);
}
.login-card h1 {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  margin: 0 0 8px;
}
.error { color: #dc2626; font-size: 13px; margin: 0; text-align: center; }

/* Layout */
.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 20px calc(80px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 24px 0;
}
.brand { font-weight: 600; letter-spacing: -0.01em; }
.topbar nav { display: flex; gap: 4px; }
.navlink {
  border: 0;
  background: none;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.navlink:hover { color: var(--text); }
.navlink.active { background: var(--text); color: var(--accent-text); }

/* Forms */
input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
  min-width: 0;
  margin: 0;
  -webkit-appearance: none;
  appearance: none;
}
input[type="date"] {
  display: flex;
  align-items: center;
  min-height: 40px;
  text-align: left;
}
input[type="date"]::-webkit-date-and-time-value { text-align: left; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23737373' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
input[type="checkbox"] { -webkit-appearance: checkbox; appearance: checkbox; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--text);
  outline-offset: -1px;
}
label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 4px;
}

.btn {
  font: inherit;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover { background: #f5f5f5; }
.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}
.btn-primary:hover { background: #262626; }

.entry-form {
  display: grid;
  grid-template-columns: 1fr 1fr 90px auto;
  gap: 10px;
  align-items: end;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.entry-form .field { min-width: 0; }

/* Stats */
.stats {
  display: flex;
  gap: 32px;
  padding: 20px 4px;
}
.stat { min-width: 0; }
.stat .stat-label { font-size: 12px; color: var(--muted); }
.stat .stat-value { font-size: 20px; font-weight: 600; letter-spacing: -0.02em; }
.stat .stat-sub { font-size: 12px; color: var(--muted); }

/* Entry list */
.entry-list { display: flex; flex-direction: column; }
summary.month-header {
  cursor: pointer;
  list-style: none;
  user-select: none;
  justify-content: flex-start;
}
summary.month-header span:last-child { margin-left: auto; }
summary.month-header::-webkit-details-marker { display: none; }
summary.month-header::before {
  content: "▸";
  display: inline-block;
  margin-right: 6px;
  transition: transform 0.15s;
}
details.month[open] > summary.month-header::before { transform: rotate(90deg); }
.month-header {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 24px 4px 8px;
  display: flex;
  justify-content: space-between;
}
.entry-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.entry-row:hover { background: rgba(0, 0, 0, 0.02); }
.entry-row .e-date { width: 110px; flex-shrink: 0; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.entry-row .e-date strong { color: var(--text); font-weight: 500; }
.entry-row .e-company { flex: 1; min-width: 0; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-row .e-hours { font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.entry-row .e-amount { width: 90px; flex-shrink: 0; text-align: right; font-weight: 500; font-variant-numeric: tabular-nums; white-space: nowrap; }
.entry-row .e-delete {
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 6px;
  visibility: hidden;
}
.entry-row:hover .e-delete, .entry-row:focus-within .e-delete { visibility: visible; }
@media (hover: none) {
  .entry-row .e-delete { visibility: visible; }
  .entry-row:hover { background: none; }
  .btn:hover { background: var(--surface); }
  .btn-primary:hover { background: var(--accent); }
}
.entry-row .e-delete:hover { color: #dc2626; background: rgba(220, 38, 38, 0.08); }
.entry-row.today .e-date strong { text-decoration: underline; text-underline-offset: 3px; }
.entry-row .e-paid {
  font-size: 11px;
  font-weight: 500;
  color: #16a34a;
  background: rgba(22, 163, 74, 0.08);
  padding: 1px 8px;
  border-radius: 999px;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 48px 0;
}

/* Invoice form */
.invoice-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 24px;
}
.invoice-form .field-wide { grid-column: span 2; }
.invoice-form .field-check { align-self: center; }
.invoice-form .field-check label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
  margin: 0;
}
.invoice-form .field-check input { width: auto; }
.invoice-form .btn { grid-column: span 2; justify-self: start; }

.invoice-actions { display: flex; justify-content: flex-end; gap: 8px; margin-bottom: 12px; }

/* Invoice document */
.invoice {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 48px;
}
.invoice h2 {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0;
}
.invoice .inv-meta { color: var(--muted); font-size: 13px; margin-top: 4px; }
.invoice .inv-parties {
  display: flex;
  gap: 48px;
  margin: 32px 0;
  font-size: 13px;
  white-space: pre-line;
}
.invoice .inv-parties h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 0 0 6px;
}
.invoice table { width: 100%; border-collapse: collapse; font-size: 14px; }
.invoice th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 500;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.invoice td { padding: 8px 0; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.invoice th:not(:first-child), .invoice td:not(:first-child) { text-align: right; }
.invoice .inv-total-row td { border-bottom: 0; font-weight: 600; padding-top: 16px; }

/* Company list */
.company-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 24px; }
.company-row {
  display: grid;
  grid-template-columns: 1fr 100px 90px auto;
  gap: 10px;
  align-items: center;
}
.company-form {
  display: grid;
  grid-template-columns: 1fr 100px 90px auto;
  gap: 10px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* Mobile */
@media (max-width: 600px) {
  .app { padding-left: 16px; padding-right: 16px; }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--bg);
    padding: calc(12px + env(safe-area-inset-top)) 0 12px;
  }
  .topbar nav {
    gap: 0;
    padding: 3px;
    background: rgba(0, 0, 0, 0.05);
    border-radius: 999px;
  }
  .navlink { padding: 7px 14px; }

  input, select, textarea, .btn {
    font-size: 16px;
    min-height: 44px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
  label { margin-bottom: 6px; }

  .entry-form {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
  .entry-form .field:first-child { grid-column: span 2; }
  .entry-form .btn { grid-column: span 2; margin-top: 4px; }

  .stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 16px 0;
  }
  .stat {
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
  }
  .stat .stat-value { font-size: 22px; }

  .month-header { margin: 16px 0 4px; padding: 8px 0; }

  .entry-row {
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    column-gap: 10px;
    row-gap: 2px;
    padding: 12px 0;
    align-items: center;
  }
  .entry-row .e-date { grid-column: 1; grid-row: 1; width: auto; }
  .entry-row .e-date strong { font-weight: 600; }
  .entry-row .e-amount { grid-column: 2 / span 3; grid-row: 1; width: auto; }
  .entry-row .e-company { grid-column: 1; grid-row: 2; font-weight: 400; color: var(--muted); font-size: 13px; }
  .entry-row .e-hours { grid-column: 2; grid-row: 2; font-size: 13px; }
  .entry-row .e-paid { grid-column: 3; grid-row: 2; }
  .entry-row .e-delete { grid-column: 4; grid-row: 2; padding: 0 4px; line-height: 1; }

  .invoice-form { grid-template-columns: 1fr; gap: 12px; }
  .invoice-form .field-wide { grid-column: auto; }
  .invoice-form .btn { grid-column: auto; justify-self: stretch; }

  .invoice-actions { flex-direction: column-reverse; }
  .invoice-actions .btn { width: 100%; }

  .invoice { padding: 20px; }
  .invoice h2 { font-size: 20px; }
  .invoice .inv-parties { flex-direction: column; gap: 20px; margin: 24px 0; }
  .invoice table { font-size: 13px; }

  .company-row, .company-form { grid-template-columns: 1fr 1fr; }
  .company-row .c-name, .company-form #company-name { grid-column: span 2; }
  .company-row .btn, .company-form .btn { grid-column: span 2; }
}

/* Print: only the invoice */
@media print {
  body { background: #fff; }
  .topbar, #view-invoice .invoice-form, .invoice-actions, #view-log, #view-settings { display: none !important; }
  .app { max-width: none; padding: 0; }
  .invoice { border: 0; border-radius: 0; padding: 0; }
}
