:root {
  /* 元の縦・横レイアウトを基準に、固定寸法も画面に対して同比率で拡縮する。 */
  --layout-width: 800;
  --layout-height: 600;
  font-size: min(
    16px,
    calc(100vw * 16 / var(--layout-width)),
    calc(100dvh * 16 / var(--layout-height))
  );
  color-scheme: dark;
  font-family: "JetBrains Mono", monospace;
  font-synthesis: none;
  --background: #111111;
  --foreground: #f1f1f1;
  --progress: #1682f6;
  --remaining: 100%;
  --percentage-position: 0.625rem;
  --elapsed-percentage-position: calc(100% + 0.3125rem);
  --elapsed-percentage-lift: 0px;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--background);
}

body {
  height: 100dvh;
  overscroll-behavior: none;
  font-size: 1.5em;
}

.timer {
  position: relative;
  isolation: isolate;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: var(--foreground);
  background: var(--background);
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.timer::after {
  content: "";
  position: absolute;
  z-index: 2;
  inset: 0;
  background: rgb(17 17 17 / 80%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease-out;
}

.timer:focus-visible {
  outline: 0.125rem solid var(--foreground);
  outline-offset: -0.125rem;
}

.progress {
  position: absolute;
  z-index: 0;
  inset: 0;
  pointer-events: none;
  opacity: 1;
}

.progress__fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--remaining);
  background: var(--progress);
  opacity: 0;
  will-change: width;
}

.progress__label,
.progress__label--elapsed {
  position: absolute;
  color: var(--foreground);
  font-family: "M PLUS 1 Code", monospace;
  font-size: 1em;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  white-space: nowrap;
}

.progress__label {
  right: auto;
  bottom: max(0.3125rem, env(safe-area-inset-bottom));
  left: var(--percentage-position);
  will-change: left;
}

.progress__label--elapsed {
  bottom: calc(max(0.3125rem, env(safe-area-inset-bottom)) + var(--elapsed-percentage-lift));
  left: var(--elapsed-percentage-position);
  opacity: 0.3;
  will-change: left, bottom;
}

.time {
  position: absolute;
  z-index: 1;
  inset: 0;
  display: grid;
  grid-auto-flow: column;
  place-content: center;
  align-items: baseline;
  column-gap: clamp(0.875rem, 1.6vw, 1.375rem);
  font-size: min(105dvh, 56vw);
  /* 字間を詰めた分の余白を補正する。 */
  padding-inline-end: 0.08em;
  pointer-events: none;
}

@media (aspect-ratio >= 1/1) {
  .time {
    /* JetBrains Monoの字形と行ボックスの中心の差を補正する。 */
    padding-block-start: 0.375rem;
  }

  .timer .setting-guide__set-time {
    /* 固定サイズの操作アイコンの領域へ、説明文が入り込まないようにする。 */
    max-width: calc(100vw - var(--guide-content-left, 65.5%) - max(5px, env(safe-area-inset-right)) - 30px - 0.375rem);
  }
}

.timer[data-digits="3"] .time {
  font-size: min(95dvh, 42vw);
}

.time__minutes,
.time__seconds {
  position: relative;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.08em;
}

.time__minutes {
  max-width: 94vw;
  font-size: 1em;
  line-height: 0.76;
  text-align: center;
}

.time__seconds {
  font-size: 0.5em;
  line-height: 0.78;
}

.time__minutes::after,
.time__seconds::after,
.progress__label::after {
  position: absolute;
  font-family: "JetBrains Mono", monospace;
  font-weight: 400;
  letter-spacing: -0.02em;
  line-height: 1;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease-out;
}

.time__minutes::after,
.time__seconds::after {
  right: -0.2em;
  bottom: -0.18em;
  left: auto;
  color: #808080;
  font-size: clamp(3.5rem, 9vw, 8.5rem);
  transform: none;
}

.time__minutes::after {
  content: "min";
}

.time__seconds::after {
  content: "sec";
}

.progress__label::after {
  content: "remaining";
  right: 0;
  bottom: calc(100% + 0.3125rem);
  color: inherit;
  font-size: 0.9em;
}

.timer[data-state="setting"][data-guide="visible"] .time__minutes::after,
.timer[data-state="setting"][data-guide="visible"] .time__seconds::after {
  opacity: 1;
}

.timer[data-state="setting"][data-guide="visible"] .progress__label::after {
  opacity: 0.5;
}

.setting-guide {
  position: absolute;
  z-index: 4;
  inset: 0;
  --guide-triangle-width: clamp(5.75rem, 10vw, 9.25rem);
  --guide-triangle-height: clamp(2.375rem, 4vmin, 3.625rem);
  color: #f1f1f1;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 200ms ease-out,
    visibility 0s linear 200ms;
}

.timer[data-state="setting"][data-guide="visible"] .setting-guide {
  visibility: visible;
  opacity: 1;
  transition-delay: 0s;
}

.setting-guide__adjust,
.setting-guide__start {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.setting-guide__adjust {
  top: max(0.5rem, env(safe-area-inset-top));
}

.setting-guide__start {
  top: auto;
  bottom: max(0.375rem, env(safe-area-inset-bottom));
  padding: 0;
  background: transparent;
}

.setting-guide__triangle {
  --guide-triangle-direction: -1;
  display: block;
  width: var(--guide-triangle-width);
  height: var(--guide-triangle-height);
  fill: none;
  stroke: currentColor;
  stroke-width: 2px;
  stroke-linejoin: round;
  stroke-linecap: round;
  opacity: 0.65;
}

.setting-guide__triangle path {
  vector-effect: non-scaling-stroke;
}

.setting-guide__triangle--down {
  --guide-triangle-direction: 1;
}

.timer[data-state="setting"][data-guide="visible"] .setting-guide__triangle {
  animation:
    guide-triangle-drift 2s cubic-bezier(0.22, 0.45, 0.35, 1) infinite,
    guide-triangle-fade 2s linear infinite;
}

/* 小さく外へ流れて自然に減速。移動とフェードを分け、急な加速を避ける。 */
@keyframes guide-triangle-drift {
  from {
    transform: translateY(calc(-16% * var(--guide-triangle-direction)));
  }

  to {
    transform: translateY(calc(10% * var(--guide-triangle-direction)));
  }
}

@keyframes guide-triangle-fade {
  0%, 100% {
    opacity: 0;
  }

  18%, 55% {
    opacity: 0.65;
  }
}

.setting-guide__message {
  position: absolute;
  left: var(--guide-content-left, 65.5%);
  bottom: calc(100% - var(--guide-content-top, 55%) + clamp(2.625rem, calc(2.4dvh + 1.25rem), 3.125rem));
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  color: rgb(241 241 241 / 52%);
  font-size: clamp(1.125rem, 1.55vw, 1.875rem);
  font-weight: 400;
  letter-spacing: -0.05em;
  line-height: 1.28;
  white-space: nowrap;
}

.setting-guide__intro {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-bottom: 0.34em;
  line-height: 1.2;
}

.setting-guide__title {
  position: relative;
  isolation: isolate;
  padding-inline: 0.12em;
  color: #ffffff;
}

.setting-guide__title::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 0;
  background: var(--progress);
  transform-origin: left center;
  animation: title-background-reveal 750ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes title-background-reveal {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

.setting-guide__set-time {
  display: block;
  max-width: calc(100vw - var(--guide-content-left, 65.5%) - 1rem);
  margin-bottom: 0.12em;
  white-space: normal;
}

.setting-guide__tap {
  display: block;
}

.pause-ui {
  position: absolute;
  z-index: 3;
  inset: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto minmax(min-content, 1fr);
  place-items: center;
  /* 小文字の見た目の中心を、行ボックスではなく画面中央へ合わせる。 */
  padding-block-end: 0.32em;
  font-size: min(52dvh, 34vw);
  line-height: 0.8;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transform: scale(1.035);
  transition:
    opacity 190ms ease-out,
    transform 230ms cubic-bezier(0.22, 1, 0.36, 1),
    visibility 0s linear 230ms;
}

.timer[data-state="paused"] .pause-ui {
  visibility: visible;
  opacity: 1;
  transform: scale(1);
  transition-delay: 0s;
}

.pause-overlay {
  grid-row: 2;
  color: #f1f1f1;
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  letter-spacing: -0.08em;
  /* pの下端まで行内に収め、再開の説明との重なりを防ぐ。 */
  line-height: 1;
  padding-inline-end: 0.08em;
  opacity: 0.9;
  pointer-events: none;
}

.pause-actions {
  grid-row: 3;
  align-self: start;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: clamp(12px, 2dvh, 24px);
  pointer-events: none;
}

.resume-hint {
  color: #f1f1f1;
  font-family: "JetBrains Mono", monospace;
  font-size: clamp(14px, 2vw, 22px);
  font-weight: 400;
  letter-spacing: 0.01em;
  line-height: 1.2;
  white-space: nowrap;
  opacity: 0.55;
  pointer-events: none;
}

.reset-trigger {
  margin-top: clamp(16px, 3dvh, 36px);
  width: clamp(132px, 16vw, 160px);
  min-height: 48px;
  padding: 0.65em 1.6em;
  border: 1px solid rgb(241 241 241 / 72%);
  border-radius: 0;
  color: #f1f1f1;
  background: rgb(17 17 17 / 18%);
  font: 700 16px / 1 "JetBrains Mono", monospace;
  letter-spacing: 0.02em;
  opacity: 0.9;
  pointer-events: none;
  cursor: pointer;
  transition: background-color 120ms ease-out;
}

.timer[data-state="paused"] .reset-trigger {
  pointer-events: auto;
}

.reset-trigger:hover {
  opacity: 1;
  background: rgb(241 241 241 / 12%);
}

.reset-trigger:focus-visible,
.reset-dialog__actions button:focus-visible {
  outline: 2px solid #f1f1f1;
  outline-offset: 3px;
}

.timer[data-state="paused"]::after {
  opacity: 1;
}

.timer[data-state="running"] .progress__fill,
.timer[data-state="paused"] .progress__fill {
  opacity: 1;
}

.timer[data-state="finished"] {
  --background: #f1f1f1;
  --foreground: #111111;
  --remaining: 0%;
  color-scheme: light;
}

.timer[data-state="finished"] .progress__fill {
  background: transparent;
}

.reset-dialog {
  width: min(calc(100vw - 32px), 420px);
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid rgb(241 241 241 / 58%);
  border-radius: 0;
  color: #f1f1f1;
  background: #111111;
  box-shadow: 0 18px 70px rgb(0 0 0 / 55%);
  font-family: "JetBrains Mono", monospace;
}

.reset-dialog::backdrop {
  background: rgb(0 0 0 / 72%);
}

.reset-dialog form {
  padding: clamp(16px, 4vmin, 28px);
}

.reset-dialog p {
  margin: 0 0 20px;
  font-size: 18px;
  line-height: 1.6;
  text-align: center;
}

.reset-dialog__actions {
  display: flex;
  gap: 12px;
}

.reset-dialog__actions button {
  flex: 1;
  min-height: 48px;
  padding: 0.7em 1em;
  border: 1px solid rgb(241 241 241 / 58%);
  border-radius: 0;
  color: #f1f1f1;
  background: transparent;
  font: 700 16px / 1.25 "M PLUS 1 Code", monospace;
  word-break: keep-all;
  overflow-wrap: normal;
  cursor: pointer;
  touch-action: manipulation;
  white-space: normal;
}

.reset-dialog__actions button:last-child {
  border-color: #d9363e;
  color: #ffffff;
  background: #d9363e;
}

.reset-dialog__actions button:hover {
  border-color: #f1f1f1;
}

.reset-dialog__actions button:last-child:hover {
  border-color: #ef5961;
  background: #ef5961;
}

@media (aspect-ratio < 1/1) {
  :root {
    --layout-width: 390;
    --layout-height: 844;
  }

  body {
    font-size: 1.05rem;
  }

  .progress__fill {
    inset: auto 0 0;
    width: 100%;
    height: var(--remaining);
    will-change: height;
  }

  .progress__label,
  .progress__label--elapsed {
    bottom: auto;
    left: max(0.5rem, env(safe-area-inset-left));
    will-change: top;
  }

  .progress__label {
    top: var(--percentage-position);
  }

  .progress__label--elapsed {
    top: var(--elapsed-percentage-position);
  }

  .time {
    grid-auto-flow: row;
    justify-items: stretch;
    gap: clamp(1rem, 4.6vw, 1.375rem);
    align-items: center;
    font-size: min(44dvh, 76vw);
    padding-inline-start: 0;
    padding-inline-end: 0;
  }

  .timer[data-digits="3"] .time {
    font-size: min(40dvh, 55vw);
  }

  .time__minutes {
    max-width: none;
    line-height: 0.76;
    /* 秒の行は動かさず、分の字形の中心だけを補正する。 */
    padding-inline-start: 0.75rem;
    padding-inline-end: 0.1em;
  }

  .time__seconds {
    font-size: min(0.667em, 29dvh);
    padding-inline-end: 0.08em;
    text-align: center;
  }

  .timer[data-digits="3"] .time__seconds {
    font-size: 0.75em;
  }

  .time__minutes::after,
  .time__seconds::after {
    right: -0.15em;
    font-size: clamp(2.875rem, 14vw, 3.75rem);
  }

  .progress__label::after {
    top: calc(100% + 0.3125rem);
    right: auto;
    bottom: auto;
    left: 0;
  }

  .setting-guide__message {
    display: contents;
  }

  .setting-guide__intro {
    position: absolute;
    top: calc(max(0.5rem, env(safe-area-inset-top)) + var(--guide-triangle-height) + 1.25rem);
    left: 50%;
    align-items: center;
    gap: 0.28em;
    margin-bottom: 0;
    font-size: clamp(0.9375rem, 4vw, 1.75rem);
    line-height: 1.28;
    transform: translateX(-50%);
  }

  .setting-guide__set-time,
  .setting-guide__tap {
    position: absolute;
    left: 50%;
    width: calc(100% - 2rem);
    margin-bottom: 0;
    font-size: clamp(0.9375rem, 4vw, 1.1875rem);
    letter-spacing: -0.06em;
    text-align: center;
    white-space: normal;
    transform: translateX(-50%);
  }

  .setting-guide__tap {
    bottom: calc(max(0.375rem, env(safe-area-inset-bottom)) + var(--guide-triangle-height) + 1.25rem);
  }

  .setting-guide__set-time {
    bottom: calc(max(0.375rem, env(safe-area-inset-bottom)) + var(--guide-triangle-height) + 3.125rem);
  }

  .pause-ui {
    font-size: min(22dvh, 34vw);
  }

  .resume-hint {
    font-size: clamp(14px, 4.5vw, 22px);
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* 説明や数字と重なるときは、文字のクリック領域も一緒に取り除く。 */
#settings-panel.has-hidden-labels .settings-control {
  grid-template-columns: var(--control-size);
  column-gap: 0;
}

#settings-panel.has-hidden-labels .settings-control > span {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .setting-guide__title::before {
    animation: none;
  }

  .timer[data-state="setting"][data-guide="visible"] .setting-guide__triangle {
    animation: none;
  }

  .timer::after,
  .setting-guide,
  .pause-ui,
  .reset-trigger {
    transition: none;
  }
}
