:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e3e5e9;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --primary: #2f6f4e;
  --primary-hover: #255a3f;
  --danger: #c0392b;
  --danger-hover: #a5301f;
  --positive: #2f6f4e;
  --negative: #c0392b;
  --info: #2b6cb0;
  --warning: #b7791f;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1e2126;
    --border: #2c3038;
    --text: #eceef1;
    --text-muted: #9aa1ab;
    --primary: #4caf7d;
    --primary-hover: #5fc490;
    --danger: #e0685a;
    --danger-hover: #ec7a6d;
    --positive: #4caf7d;
    --negative: #e0685a;
    --info: #5b9bd5;
    --warning: #d9a441;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 1px 2px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

.screen { min-height: 100vh; }

/* Login */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 28px;
  width: 100%;
  max-width: 340px;
  text-align: center;
}

.login-card h1 { margin: 0 0 4px; font-size: 1.4rem; }

.subtitle { color: var(--text-muted); font-size: 0.9rem; margin: 0 0 20px; }

.login-card input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 14px;
}

.error-text { color: var(--negative); font-size: 0.85rem; margin: 10px 0 0; }

/* Buttons */
button {
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.btn-primary { background: var(--primary); color: #fff; width: 100%; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }

.btn-danger { background: transparent; color: var(--danger); }
.btn-danger:hover { color: var(--danger-hover); text-decoration: underline; }

/* App layout */
.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 24px 20px 8px;
  max-width: 900px;
  margin: 0 auto;
}

.header-title h1 { margin: 0; font-size: 1.5rem; }

.report {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  max-width: 900px;
  margin: 16px auto;
  padding: 0 20px;
}

.report-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.report-label { font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.report-value { font-size: 1.8rem; font-weight: 700; letter-spacing: -0.01em; }
.report-hint { font-size: 0.75rem; color: var(--text-muted); }

.report-card-primary {
  padding: 20px 16px;
  border-color: var(--primary);
  border-width: 2px;
}

.report-card-primary .report-label { font-size: 0.85rem; }
.report-card-primary .report-value { font-size: 2.6rem; line-height: 1.1; }

@media (max-width: 480px) {
  .report-card-primary .report-value { font-size: 2.1rem; }
}

.btn-small { align-self: flex-start; padding: 4px 10px; font-size: 0.75rem; margin-top: 2px; }

.phases-section {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 20px 60px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 20px 0 12px;
}

.section-header h2 { margin: 0; font-size: 1.1rem; }
.section-header .btn-primary { width: auto; }

.section-header-title { display: flex; align-items: center; gap: 10px; }

.overdue-badge {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--negative);
  background: color-mix(in srgb, var(--negative) 15%, transparent);
  border: 1px solid var(--negative);
  border-radius: 999px;
  padding: 3px 10px;
}

.phases-list { display: flex; flex-direction: column; gap: 10px; }

.phase-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
}

.phase-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.phase-card:hover { border-color: var(--primary); }

.phase-card.dragging { box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); border-color: var(--primary); }

.phase-card.excluded { opacity: 0.7; }

.phase-cumulative {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
  transition: color 0.15s ease;
}

.phase-card:hover .phase-cumulative { color: var(--text); }

.phase-cumulative.over-budget { color: var(--negative); font-weight: 600; }
.phase-card:hover .phase-cumulative.over-budget { color: var(--negative); }
.phase-card.over-budget { border-color: var(--negative); }

.budget-limit-marker {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 2px 0;
  color: var(--negative);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}

.budget-limit-marker::before,
.budget-limit-marker::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--negative);
}

.phase-card-placeholder {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: transparent;
}

.drag-handle {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 36px;
  font-size: 1.3rem;
  line-height: 1;
  color: var(--text-muted);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.drag-handle:active { cursor: grabbing; color: var(--text); }

.phase-info { flex: 1; min-width: 0; text-align: left; }
.phase-title { font-weight: 600; margin: 0 0 2px; text-align: left; }
.phase-desc {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.phase-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.status-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.status-badge.status-in_progress { color: var(--info); border-color: var(--info); }
.status-badge.status-done { color: var(--positive); border-color: var(--positive); }

.deadline-text { font-size: 0.78rem; color: var(--text-muted); }
.deadline-text.overdue { color: var(--negative); font-weight: 600; }
.deadline-text.due-soon { color: var(--warning); font-weight: 600; }
.deadline-text.late { color: var(--negative); }
.deadline-text.on-time { color: var(--positive); }

.phase-prices {
  text-align: right;
  flex-shrink: 0;
  font-size: 0.85rem;
}

.phase-prices .estimated { color: var(--text-muted); }
.phase-prices .final { font-weight: 600; }
.phase-prices .final.over { color: var(--negative); }
.phase-prices .final.under { color: var(--positive); }
.phase-prices .pending { color: var(--text-muted); font-style: italic; }
.phase-prices .remaining { color: var(--text); font-size: 1rem; }
.phase-prices .exceeded { color: var(--negative); font-size: 1rem; }
.phase-prices .excluded-tag { color: var(--text-muted); font-size: 0.75rem; font-style: italic; margin-top: 2px; }

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

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}

@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
}

.modal {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px 20px;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

@media (min-width: 640px) {
  .modal { border-radius: var(--radius); }
}

.modal h2 { margin: 0 0 16px; font-size: 1.2rem; }

.modal label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.modal input, .modal textarea, .modal select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.checkbox-row {
  display: flex !important;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 0.9rem;
  margin-bottom: 6px !important;
}

.checkbox-row input {
  display: inline-block !important;
  width: auto !important;
  margin-top: 0 !important;
}

.field-hint {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin: 0 0 16px;
}

#payments-section {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  margin-bottom: 14px;
}

.payments-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}

.payments-header h3 { margin: 0; font-size: 0.95rem; }
.payments-header span { font-size: 0.85rem; color: var(--text-muted); }

.payments-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }

.payments-empty { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 10px; }

.payment-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.payment-date { color: var(--text-muted); font-size: 0.85rem; }
.payment-amount { font-weight: 600; margin-left: auto; }

.payment-delete {
  background: transparent;
  color: var(--text-muted);
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 0.85rem;
}
.payment-delete:hover { color: var(--danger); }

.payment-add-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  margin-bottom: 14px;
}

.payment-add-row input {
  margin-top: 0;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}

.btn-secondary {
  background: var(--border);
  color: var(--text);
  white-space: nowrap;
}
.btn-secondary:hover { opacity: 0.85; }

.modal-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.modal-actions-right { display: flex; gap: 8px; }
.modal-actions-right .btn-primary { width: auto; }

@media (max-width: 480px) {
  .app-header { flex-direction: column; }
  .field-row { grid-template-columns: 1fr; }
  .payment-add-row { grid-template-columns: 1fr 1fr; }
  .payment-add-row .btn-secondary { grid-column: 1 / -1; }
}
