:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --text: #1c1c28;
  --text-dim: #6b6b7b;
  --primary: #4f46e5;
  --primary-dim: #eef0ff;
  --border: #e6e6ef;
  --danger: #e0455d;
  --done: #16a34a;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f14;
    --surface: #1b1b24;
    --text: #f0f0f5;
    --text-dim: #9797a8;
    --primary: #8180ff;
    --primary-dim: #26243f;
    --border: #2c2c3a;
    --danger: #ff6b7f;
    --done: #34d399;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
  overflow: hidden;
}

#app {
  /* iOS standalone ("Add to Home Screen") web apps have a long-documented
     WKWebView bug where CSS viewport units (vh/dvh/svh) — and even
     position:fixed's containing-block math — don't reliably match the
     actual visible height. window.innerHeight read from JS does, so app.js
     measures it and writes --app-height on load/resize; 100vh is only the
     pre-JS fallback. */
  height: 100vh;
  height: var(--app-height, 100vh);
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: calc(16px + var(--safe-top)) 20px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
  text-align: center;
}

.today-label {
  margin: 0;
  color: var(--text);
  font-size: 1.5rem;
  font-weight: 700;
}

main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}

.view[hidden] { display: none; }

/* Schedule tab fills main's content box exactly via flexbox, computed by
   the browser's own layout engine rather than re-measured in JS on every
   render — that's what made it drift/resize and allowed a hair of scroll. */
#view-cal {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Progress bar */
.progress-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.progress-bar {
  flex: 1;
  height: 10px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--done);
  transition: width 0.25s ease;
}
.progress-text {
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: nowrap;
}

/* Task list (Tasks tab) */
.task-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  cursor: pointer;
  user-select: none;
}

.task-item.done {
  background: var(--primary-dim);
}

.checkbox {
  width: 26px;
  height: 26px;
  min-width: 26px;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 0.9rem;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.task-item.done .checkbox {
  background: var(--done);
  border-color: var(--done);
}

.task-name {
  flex: 1;
  font-size: 1rem;
}

.task-item.done .task-name {
  text-decoration: line-through;
  color: var(--text-dim);
}

.task-edit-btn {
  flex: none;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.05rem;
  padding: 6px;
}

.empty-msg {
  color: var(--text-dim);
  text-align: center;
  padding: 40px 20px;
  font-size: 0.95rem;
}

/* Quick-add form (Tasks tab) */
.quick-add-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.quick-add-row {
  display: flex;
  gap: 8px;
}

.quick-add-row input {
  flex: 1;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  min-width: 0;
}

.qa-submit-btn {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 1.4rem;
  line-height: 1;
}

.quick-add-edit-actions {
  display: flex;
  gap: 10px;
}

.quick-add-edit-actions .secondary-btn {
  flex: 1;
}

/* Schedule tab */
.cal-header-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.cal-header-row .segmented {
  flex: 1;
}

.fab {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 1.3rem;
  line-height: 1;
}

.cal-timeline-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.cal-timeline {
  position: relative;
}

.hour-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px solid var(--border);
}

.hour-label {
  position: absolute;
  left: 6px;
  top: -7px;
  font-size: 0.66rem;
  color: var(--text-dim);
  background: var(--surface);
  padding: 0 4px;
  white-space: nowrap;
}

.event-layer {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 54px;
  right: 8px;
}

.event-block {
  position: absolute;
  border-radius: 8px;
  background: var(--primary-dim);
  border-left: 3px solid var(--primary);
  padding: 3px 8px;
  overflow: hidden;
  font-size: 0.78rem;
  line-height: 1.25;
  box-sizing: border-box;
  cursor: pointer;
  user-select: none;
}

.event-block .event-name {
  font-weight: 600;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.event-block .event-time {
  display: block;
  color: var(--text-dim);
  font-size: 0.7rem;
  white-space: nowrap;
}

.now-line {
  position: absolute;
  left: 54px;
  right: 8px;
  height: 2px;
  background: var(--danger);
  z-index: 4;
}

.now-line::before {
  content: "";
  position: absolute;
  left: -5px;
  top: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
}

/* Shared form controls */
.segmented {
  display: flex;
  gap: 6px;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
}

.segmented-btn {
  flex: 1;
  padding: 9px 0;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.88rem;
}

.segmented-btn.selected {
  background: var(--surface);
  color: var(--primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.time-input {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.15rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Hide the browser's own built-in clear icon on the time inputs. */
.time-input::-webkit-clear-button,
.time-input::-webkit-inner-spin-button {
  display: none;
  -webkit-appearance: none;
  margin: 0;
}

.primary-btn {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 13px;
  font-size: 1rem;
  font-weight: 600;
}

.secondary-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  padding: 11px;
  font-size: 0.9rem;
}

.danger-btn {
  color: var(--danger);
  border-color: var(--danger);
}

/* Bottom sheet (add/edit on Schedule tab) */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 20;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 10px 18px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
}

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: var(--border);
  margin: 4px auto 2px;
}

.sheet-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  text-align: center;
}

.ev-name-input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1.05rem;
}

.time-range-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.time-range-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.time-range-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.time-range-arrow {
  flex: none;
  color: var(--text-dim);
  font-size: 1.2rem;
  padding-top: 20px;
}

.sheet-actions {
  display: flex;
  gap: 10px;
}

.sheet-actions .primary-btn,
.sheet-actions .secondary-btn {
  flex: 1;
}

/* Tab bar */
.tab-bar {
  flex: none;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}

.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 0 8px;
  font-size: 0.75rem;
}

.tab-icon {
  width: 24px;
  height: 24px;
}

.tab-btn.active {
  color: var(--primary);
}
