:root {
  --bg: #0f172a;
  --bg-2: #1e293b;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --warning: #d97706;
  --success: #059669;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --radius: 12px;
  --radius-sm: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic UI", "Yu Gothic", "Meiryo", sans-serif;
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--bg);
  color: #fff;
  box-shadow: var(--shadow);
}
.app-header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.icon-btn {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 24px;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:active { background: rgba(255,255,255,0.1); }

.tabs {
  display: flex;
  background: #fff;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: calc(var(--safe-top) + 56px);
  z-index: 9;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 1 0 auto;
  min-width: 64px;
  background: transparent;
  border: none;
  padding: 12px 16px;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

main {
  padding: 16px;
  max-width: 720px;
  margin: 0 auto;
}

.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h2 {
  margin: 0 0 12px;
  font-size: 16px;
  font-weight: 600;
}
.card label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 500;
}
.card .hint {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--text-muted);
}

input[type="text"],
input[type="number"],
input[type="date"],
select {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: inherit;
  background: #fff;
  margin-bottom: 12px;
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

button {
  font-family: inherit;
  font-size: 15px;
  font-weight: 500;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: background 0.15s;
}
button.primary {
  width: 100%;
  background: var(--primary);
  color: #fff;
}
button.primary:active { background: var(--primary-hover); }
button.secondary {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  margin-bottom: 8px;
}
button.danger {
  background: var(--danger);
  color: #fff;
}
button.ghost {
  background: transparent;
  color: var(--text-muted);
}

.list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
}
.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  background: #fff;
}
.list-item-main {
  flex: 1;
  min-width: 0;
}
.list-item-title {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-item-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
}
.list-item-amount {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
}
.list-item-amount.negative { color: var(--danger); }
.list-item-amount.positive { color: var(--success); }
.list-empty {
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

.kpi {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.kpi-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}
.kpi-row span {
  font-size: 13px;
  color: var(--text-muted);
}
.kpi-row strong {
  font-size: 16px;
  font-weight: 600;
}
.kpi-row.warning {
  background: #fef3c7;
}
.kpi-row.warning strong {
  color: var(--warning);
}
.kpi-row .negative { color: var(--danger); }
.kpi-row .positive { color: var(--success); }

.chart-wrap {
  position: relative;
  width: 100%;
  height: 280px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-muted);
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}
.legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.events {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 360px;
  overflow-y: auto;
}
.event-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.event-row:last-child { border-bottom: none; }
.event-date {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  white-space: nowrap;
}
.event-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.event-amount {
  font-weight: 600;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.event-amount.negative { color: var(--danger); }
.event-amount.positive { color: var(--success); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}
.modal-content {
  background: #fff;
  width: 100%;
  max-width: 560px;
  border-radius: 16px 16px 0 0;
  padding: 20px 16px calc(env(safe-area-inset-bottom, 0px) + 20px);
  max-height: 85vh;
  overflow-y: auto;
}
.modal-content h2 {
  margin: 0 0 16px;
  font-size: 18px;
  font-weight: 600;
}
.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
.modal-actions button { flex: 1; }

#edit-form .field { margin-bottom: 12px; }
#edit-form .field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
#edit-form .radio-group {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
#edit-form .radio-group label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin: 0;
  font-size: 14px;
}
#edit-form .radio-group input { display: none; }
#edit-form .radio-group input:checked + span {
  color: var(--primary);
  font-weight: 600;
}
#edit-form .radio-group label:has(input:checked) {
  border-color: var(--primary);
  background: rgba(37, 99, 235, 0.06);
}

.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 24px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg);
  color: #fff;
  padding: 10px 16px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 200;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

@media (min-width: 640px) {
  .tab { flex: 0 0 auto; }
}
