:root {
  --bg: #FAFDFB;
  --surface: #FFFFFF;
  --surface-low: #F3F7F5;
  --surface-high: #E7EFEC;
  --primary: #0E6B66;
  --on-primary: #FFFFFF;
  --primary-container: #BCECE8;
  --on-primary-container: #00201E;
  --text: #171D1C;
  --text-muted: #52615E;
  --outline: #CFD8D6;
  --tick: #16A34A;
  --tick-bg: rgba(22, 163, 74, 0.15);
  --cross: #DC2626;
  --cross-bg: rgba(220, 38, 38, 0.15);
  --cell-size: 64px;
  --name-col-width: 140px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F1514;
    --surface: #17201E;
    --surface-low: #1C2624;
    --surface-high: #25312F;
    --primary: #54D6CE;
    --on-primary: #003734;
    --primary-container: #00504C;
    --on-primary-container: #BCECE8;
    --text: #E0E6E4;
    --text-muted: #9BA8A5;
    --outline: #32403E;
    --tick: #4ADE80;
    --tick-bg: rgba(74, 222, 128, 0.18);
    --cross: #F87171;
    --cross-bg: rgba(248, 113, 113, 0.18);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  overflow: hidden;
  height: 100dvh;
}

.hidden {
  display: none !important;
}

/* Fullscreen Gate Screens (Lock & Auth) */
.state-screen {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.state-card {
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: 26px;
  padding: 32px 24px;
  max-width: 400px;
  width: 100%;
  text-align: center;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
}

.state-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin: 0 auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.locked-icon {
  background: var(--cross-bg);
  color: var(--cross);
}

.auth-icon {
  background: var(--primary-container);
  color: var(--on-primary-container);
}

.state-card h1 {
  font-size: 1.4rem;
  margin: 0 0 10px;
  font-weight: 800;
}

.state-card p {
  font-size: 0.95rem;
  color: var(--text-muted);
  line-height: 1.45;
  margin: 0 0 24px;
}

.text-input {
  width: 100%;
  height: 52px;
  border-radius: 14px;
  border: 1.5px solid var(--outline);
  background: var(--surface-low);
  color: var(--text);
  padding: 0 16px;
  font-size: 1rem;
  margin-bottom: 12px;
}

.passcode-input {
  font-size: 1.35rem;
  letter-spacing: 6px;
  text-align: center;
  font-weight: 700;
}

.error-banner {
  background: var(--cross-bg);
  color: var(--cross);
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 12px;
}

.btn-primary {
  width: 100%;
  height: 54px;
  border: none;
  border-radius: 16px;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

/* Main Ledger Layout */
.ledger-app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px 8px;
  background: linear-gradient(180deg, var(--primary-container) 0%, transparent 100%);
}

.top-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 800;
}

.status-pill {
  font-size: 0.72rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  text-transform: uppercase;
}

.status-pill.online {
  background: var(--tick-bg);
  color: var(--tick);
}

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

.btn-tonal {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--outline);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 0.85rem;
}

.btn-tonal:disabled {
  opacity: 0.4;
}

/* Days Bar */
.days-bar {
  margin: 4px 16px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.days-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.cal-icon {
  color: var(--primary);
}

.days-title {
  font-weight: 700;
  font-size: 0.95rem;
}

.days-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.changes-badge {
  background: var(--primary-container);
  color: var(--on-primary-container);
  padding: 6px 12px;
  border-radius: 12px;
  font-size: 0.78rem;
  font-weight: 700;
}

/* Sticky Grid */
.grid-wrapper {
  flex: 1;
  padding: 6px 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.grid-card {
  flex: 1;
  border: 1px solid var(--outline);
  border-radius: 22px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.grid-header-row {
  display: flex;
  background: var(--surface-high);
  border-bottom: 1px solid var(--outline);
  height: 68px;
  flex-shrink: 0;
}

.sticky-name-header {
  width: var(--name-col-width);
  flex-shrink: 0;
  padding: 0 12px;
  display: flex;
  align-items: center;
  font-weight: 700;
  font-size: 0.88rem;
  color: var(--text-muted);
  border-right: 1px solid var(--outline);
}

.days-header-track {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
}
.days-header-track::-webkit-scrollbar {
  display: none;
}

.day-col-header {
  width: var(--cell-size);
  height: 68px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px;
}

.day-col-header.today .day-pill {
  background: var(--primary);
  color: var(--on-primary);
}

.day-pill {
  width: 54px;
  height: 56px;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.day-dow, .day-mon {
  font-size: 0.68rem;
  opacity: 0.85;
}

.day-num {
  font-size: 1rem;
  font-weight: 800;
}

.grid-body {
  flex: 1;
  overflow-y: auto;
}

.item-row {
  display: flex;
  height: var(--cell-size);
  border-bottom: 1px solid var(--outline);
}

.item-row:nth-child(even) {
  background: var(--surface-low);
}

.item-name-cell {
  width: var(--name-col-width);
  flex-shrink: 0;
  padding: 0 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border-right: 1px solid var(--outline);
}

.item-title {
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.2;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.item-cells-track {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
}
.item-cells-track::-webkit-scrollbar {
  display: none;
}

.ledger-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
}

.ledger-cell.highlighted {
  background: rgba(14, 107, 102, 0.06);
}

.ledger-cell.dirty::after {
  content: "";
  position: absolute;
  top: 6px;
  right: 6px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #F59E0B;
}

.dot-empty {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--outline);
}

.badge-tick {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--tick-bg);
  color: var(--tick);
  display: flex;
  align-items: center;
  justify-content: center;
}

.badge-cross {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--cross-bg);
  color: var(--cross);
  display: flex;
  align-items: center;
  justify-content: center;
}

.badge-number {
  min-width: 38px;
  height: 32px;
  border-radius: 10px;
  background: var(--primary-container);
  color: var(--on-primary-container);
  font-weight: 800;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Bottom Submit Dock */
.submit-dock {
  background: var(--surface);
  border-top: 1px solid var(--outline);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dock-meta {
  display: flex;
  flex-direction: column;
}

#dock-summary {
  font-weight: 700;
  font-size: 0.9rem;
}

.dock-note {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.btn-submit {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 50px;
  padding: 0 20px;
  border-radius: 16px;
  border: none;
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 800;
  font-size: 0.95rem;
  cursor: pointer;
}

.btn-submit:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Bottom Sheet Number Pad */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  z-index: 100;
}

.picker-sheet {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 28px 28px 0 0;
  padding: 12px 20px calc(24px + env(safe-area-inset-bottom));
  animation: slideUp 0.2s ease-out;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--outline);
  border-radius: 99px;
  margin: 0 auto 14px;
}

.sheet-title {
  margin: 0 0 14px;
  font-size: 1.2rem;
  font-weight: 800;
}

.sheet-status-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 18px;
}

.choice-btn {
  height: 60px;
  border-radius: 18px;
  border: 1.5px solid var(--outline);
  background: var(--surface-low);
  color: var(--text);
  font-weight: 700;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.choice-tick.active {
  background: var(--tick);
  color: #fff;
  border-color: var(--tick);
}

.choice-cross.active {
  background: var(--cross);
  color: #fff;
  border-color: var(--cross);
}

.sheet-section-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.number-pad-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.num-btn {
  height: 52px;
  border-radius: 16px;
  border: 1px solid var(--outline);
  background: var(--surface-low);
  color: var(--text);
  font-size: 1.1rem;
  font-weight: 700;
}

.num-btn.active {
  background: var(--primary);
  color: var(--on-primary);
}

.btn-clear-cell {
  width: 100%;
  height: 48px;
  border: none;
  background: transparent;
  color: var(--cross);
  font-weight: 700;
  font-size: 0.95rem;
}

.toast {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: 99px;
  font-weight: 700;
  font-size: 0.88rem;
  z-index: 200;
}