:root {
  --bg: #f5f5f5;
  --panel: #ffffff;
  --text: #151515;
  --muted: #666;
  --border: #ddd;
  --active: #f6c453;
}

body.dark {
  --bg: #111;
  --panel: #1b1b1b;
  --text: #f2f2f2;
  --muted: #aaa;
  --border: #333;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.3s, color 0.3s;
}

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

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  margin-bottom: 16px;
}

h1,
h2 {
  font-size: 1.4rem;
  margin: 0 0 12px;
}

h2 {
  margin-top: 18px;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin: 10px 0 4px;
}

input,
select,
button {
  width: 100%;
  font: inherit;
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 10px;
  background: var(--panel);
  color: var(--text);
}

input[type="color"] {
  min-height: 44px;
  padding: 6px;
}

button,
summary {
  cursor: pointer;
  font-weight: 600;
}

summary {
  list-style: none;
}

summary::-webkit-details-marker {
  display: none;
}

summary::after {
  content: "+";
  float: right;
}

details[open] summary::after {
  content: "−";
}

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.forward-grid {
  grid-template-columns: repeat(4, 1fr);
}

.compact-button {
  width: auto;
  white-space: nowrap;
  padding: 8px 12px;
}

.period-display {
  position: relative;
  min-height: 320px;
  border-radius: 24px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  color: #111;
  background: var(--active);
  filter: brightness(0.72);
  transition: background 0.4s, filter var(--brighten-fade-duration, 0.8s), border-radius 0.2s;
  margin-bottom: 16px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.period-display.bright {
  filter: brightness(1.25);
}

.display-content {
  width: 100%;
}

.icon-button {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.78);
  border: 1px solid rgba(0, 0, 0, 0.18);
  color: #111;
  font-size: 1.3rem;
  line-height: 1;
  z-index: 3;
}

.period-name {
  font-size: 2.6rem;
  font-weight: 800;
  margin-bottom: 4px;
}

.period-interval {
  font-size: 1rem;
  opacity: 0.8;
  margin-bottom: 24px;
}

.time-left {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 20px;
}

.hidden {
  visibility: hidden;
}

.progress-shell {
  height: 22px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.22);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.85);
  transition: width 0.08s linear;
}

.dots {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 18px;
}

.dot {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.28);
}

.dot.active {
  background: rgba(255, 255, 255, 0.95);
}

.status {
  text-align: center;
  color: rgba(0, 0, 0, 0.65);
  margin-top: 12px;
  font-weight: 700;
}

.gesture-hint {
  margin-top: 8px;
  font-size: 0.76rem;
  color: rgba(0, 0, 0, 0.55);
}

.small {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 8px 0 0;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  color: var(--text);
}

.checkbox-row input {
  width: auto;
}

.time-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr auto;
  align-items: center;
  gap: 8px;
}

.time-row span {
  color: var(--muted);
  font-size: 0.9rem;
}

body.display-only {
  overflow: hidden;
}

body.display-only main {
  max-width: none;
  padding: 0;
}

body.display-only main > :not(#display) {
  display: none;
}

body.display-only #display {
  position: fixed;
  inset: 0;
  min-height: 100dvh;
  margin: 0;
  border-radius: 0;
  z-index: 9999;
}

body.display-only .period-name {
  font-size: clamp(3rem, 14vw, 8rem);
}

body.display-only .period-interval {
  font-size: clamp(1.2rem, 4vw, 2rem);
}

body.display-only .time-left {
  font-size: clamp(2.5rem, 10vw, 6rem);
}

body.display-only .progress-shell {
  height: clamp(24px, 4vw, 44px);
}

.setting-line {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  align-items: center;
  margin: 10px 0 4px;
}

.inline-checkbox {
  margin: 0;
  justify-content: flex-start;
  white-space: nowrap;
}

.compact-time-row {
  margin: 0;
}

.period-setting-row {
  margin-top: 14px;
}

.period-setting-row > label {
  margin-bottom: 6px;
  color: var(--text);
  font-weight: 700;
}

.period-setting-controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 72px;
  gap: 8px;
  align-items: center;
}

.period-setting-controls input[type="color"] {
  width: 72px;
  min-width: 72px;
  height: 44px;
}

input:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.warning-button {
  border-color: rgba(180, 72, 72, 0.72);
  background: rgba(120, 42, 42, 0.22);
  color: var(--text);
}

.warning-button:hover {
  background: rgba(150, 52, 52, 0.32);
}

.settings-divider {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 18px 0;
}

.controls-card .settings-divider {
  margin: 18px 0;
}

/* Themed control colors */
.controls-card {
  --current-muted-bg: var(--panel);
  --current-muted-border: var(--border);
  --next-muted-bg: var(--panel);
  --next-muted-border: var(--border);
}

.controls-card button,
.forward-grid button {
  transition: background 0.18s ease, border-color 0.18s ease, filter 0.18s ease;
}

.period-button,
.forward-grid button {
  background: var(--current-muted-bg);
  border-color: var(--current-muted-border);
  color: var(--text);
}

.period-button:hover,
.forward-grid button:hover {
  filter: brightness(1.08);
}

.run-button {
  border-color: rgba(68, 150, 86, 0.72);
  background: rgba(52, 150, 84, 0.24);
  color: var(--text);
}

.run-button:hover {
  background: rgba(52, 150, 84, 0.34);
}

.run-button.pause-state {
  background: var(--current-muted-bg);
  border-color: var(--current-muted-border);
}

.next-period-button {
  background: var(--next-muted-bg);
  border-color: var(--next-muted-border);
  color: var(--text);
}

.next-period-button:hover {
  filter: brightness(1.08);
}

/* Inline clear controls */
.input-clear-wrap {
  position: relative;
  min-width: 0;
}

.input-clear-wrap input {
  padding-right: 34px;
}

.field-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  min-height: 0;
  padding: 0;
  border-radius: 999px;
  border: 1px solid rgba(180, 72, 72, 0.42);
  background: rgba(120, 42, 42, 0.28);
  color: #ffb0b0;
  line-height: 1;
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease, background 0.18s ease;
}

.input-clear-wrap:hover .field-clear,
.input-clear-wrap:focus-within .field-clear {
  opacity: 1;
  pointer-events: auto;
}

.field-clear:hover {
  background: rgba(180, 72, 72, 0.45);
}

.color-clear-wrap input[type="color"] {
  padding-right: 6px;
}

.color-clear {
  right: -6px;
  top: -6px;
  transform: none;
}

@media (max-width: 420px) {
  .forward-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .app-header {
    align-items: stretch;
    flex-direction: column;
  }

  .compact-button {
    width: 100%;
  }

  .setting-line {
    grid-template-columns: 1fr;
  }

  .period-setting-controls {
    grid-template-columns: minmax(0, 1fr) 64px;
  }

  .period-setting-controls input[type="color"] {
    width: 64px;
    min-width: 64px;
  }
}


/* Settings cleanup */
.settings-panel label {
  font-weight: 700;
  color: var(--text);
}

.settings-panel .small + .time-row-wrapper {
  margin-top: 6px;
}

.field-clear {
  border-radius: 7px;
}

.input-clear-wrap.has-clear .field-clear {
  opacity: 0;
  pointer-events: none;
}

.input-clear-wrap.has-clear:hover .field-clear,
.input-clear-wrap.has-clear:focus-within .field-clear {
  opacity: 1;
  pointer-events: auto;
}

.field-warning {
  margin: 8px 0 0;
  font-size: 0.85rem;
  color: #f0b35c;
}

.input-invalid {
  border-color: #d6923d;
  box-shadow: 0 0 0 2px rgba(214, 146, 61, 0.16);
}

.description-gap {
  margin-bottom: 10px;
}

.period-default-actions {
  margin-top: 18px;
}

.gesture-key {
  display: inline-block;
  margin: 0 4px 0 8px;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(40, 40, 40, 0.75);
  color: rgba(255, 255, 255, 0.9);
  font-weight: 700;
}

/* Desktop popout display */
.popout-button {
  right: 64px;
}

@media (max-width: 700px) {
  #popoutBtn {
    display: none;
  }
}

body.popout-mode main {
  max-width: none;
  padding: 0;
}

body.popout-mode #display {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  border-radius: 0;
}

body.popout-mode .icon-button,
body.popout-mode .gesture-hint {
  display: none;
}

body.popout-mode .period-name {
  font-size: clamp(3rem, 14vw, 8rem);
}

body.popout-mode .period-interval {
  font-size: clamp(1.2rem, 4vw, 2rem);
}

body.popout-mode .time-left {
  font-size: clamp(2.5rem, 10vw, 6rem);
}

body.popout-mode .progress-shell {
  height: clamp(24px, 4vw, 44px);
}
