:root {
  --sand: #f5a000;
  --page: #fff7f5;
  --ink: #2c2928;
  --muted: #8f8582;
  --line: #ded9d2;
  --panel: rgba(255, 255, 255, .78);
  --shadow: 0 18px 50px rgba(82, 58, 50, .12);
  --button-shadow: rgba(245, 160, 0, .34);
  --progress: 0;
  --top-fill: .8;
  --bottom-fill: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  overflow-x: hidden;
  background: var(--page);
}

body {
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 22%, rgba(255, 255, 255, .9), transparent 32rem),
    var(--page);
}

button {
  border: 0;
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(100%, 430px);
  max-width: 100%;
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  padding: max(28px, env(safe-area-inset-top)) 22px max(22px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto auto auto auto auto auto;
  align-content: start;
  gap: 12px;
  overflow-x: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 800;
  letter-spacing: 0;
}

.icon-button {
  width: 48px;
  height: 48px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, .8);
  box-shadow: 0 8px 22px rgba(82, 58, 50, .1);
  font-size: 22px;
}

.settings-tray {
  min-height: 94px;
}

.palette-panel {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 7px;
  padding: 9px;
  border-radius: 24px;
  background: rgba(255, 255, 255, .68);
  box-shadow: 0 12px 32px rgba(82, 58, 50, .1);
  opacity: 1;
  transform: translateY(0);
  transition: opacity .18s ease, transform .18s ease;
}

.palette-panel.is-collapsed {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-4px);
}

.swatch {
  width: 100%;
  aspect-ratio: 1;
  min-width: 38px;
  border-radius: 999px;
  background: var(--swatch);
  border: 4px solid rgba(255, 255, 255, .9);
  box-shadow: inset 0 -8px 16px rgba(0, 0, 0, .08), 0 5px 14px rgba(82, 58, 50, .12);
}

.swatch.is-active {
  outline: 3px solid var(--ink);
  outline-offset: 3px;
}

.refresh-button {
  width: 100%;
  grid-column: 1 / -1;
  min-width: 38px;
  min-height: 34px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, .78);
  box-shadow: inset 0 0 0 1px rgba(222, 217, 210, .78), 0 5px 14px rgba(82, 58, 50, .1);
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.hourglass-wrap {
  display: grid;
  place-items: center;
  min-height: 0;
}

.hourglass {
  width: min(68vw, 250px);
  max-height: 34vh;
  max-height: 34svh;
  filter: drop-shadow(0 18px 26px rgba(82, 58, 50, .1));
}

.glass-fill {
  fill: rgba(255, 255, 255, .68);
}

.glass-line {
  fill: none;
  stroke: var(--line);
  stroke-width: 4;
}

.stand {
  fill: none;
  stroke: #4b4140;
  stroke-width: 10;
  stroke-linecap: round;
}

.shine {
  fill: none;
  stroke: url("#glassShine");
  stroke-width: 9;
  stroke-linecap: round;
  opacity: .8;
}

.top-sand,
.bottom-sand,
.sand-drop {
  fill: var(--sand);
}

.top-sand {
  transform-box: fill-box;
  transform-origin: 50% 100%;
  transform: scaleY(var(--top-fill));
  transition: transform .35s ease;
}

.bottom-sand {
  transform-box: fill-box;
  transform-origin: 50% 100%;
  transform: scaleY(var(--bottom-fill));
  transition: transform .35s ease;
}

.sand-drop {
  opacity: 0;
}

.is-running .sand-drop {
  opacity: .85;
  animation: fall 1.4s linear infinite;
}

.is-running .drop-two {
  animation-delay: .32s;
}

.is-running .drop-three {
  animation-delay: .64s;
}

@keyframes fall {
  0% {
    transform: translateY(-18px);
    opacity: 0;
  }
  25%,
  72% {
    opacity: .9;
  }
  100% {
    transform: translateY(35px);
    opacity: 0;
  }
}

.clock {
  margin: 0;
  text-align: center;
  font-size: clamp(44px, 15vw, 66px);
  line-height: 1;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.time-editor {
  width: min(100%, 330px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.digit-pair {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 18px;
  gap: 6px;
  align-items: center;
}

.digit-column {
  display: grid;
  gap: 6px;
  justify-items: center;
}

.step-button {
  width: 100%;
  min-width: 36px;
  height: 36px;
  border-radius: 13px;
  background: var(--panel);
  box-shadow: 0 8px 20px rgba(82, 58, 50, .08);
  color: var(--muted);
  font-size: 24px;
  line-height: 1;
}

.step-button:disabled {
  opacity: .35;
}

.digit-value {
  width: 100%;
  min-width: 36px;
  height: 46px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, .52);
  box-shadow: inset 0 0 0 1px rgba(222, 217, 210, .55);
  font-size: 32px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.unit-label {
  align-self: center;
  padding: 0 1px;
  font-size: 17px;
  color: var(--muted);
  font-weight: 700;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1.25fr;
  gap: 12px;
  align-items: center;
}

.primary-button,
.reset-button,
.ok-button {
  min-height: 58px;
  border-radius: 999px;
  font-weight: 800;
}

.primary-button,
.ok-button {
  color: white;
  background: var(--sand);
  box-shadow: 0 14px 30px var(--button-shadow);
}

.reset-button {
  background: rgba(255, 255, 255, .72);
  color: var(--muted);
  box-shadow: inset 0 0 0 1px rgba(222, 217, 210, .8);
}

.history {
  min-height: 76px;
}

.history p {
  margin: 4px 0 10px;
  color: var(--muted);
  font-size: 15px;
  font-weight: 700;
}

.history-list {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.history-chip {
  min-width: 76px;
  min-height: 42px;
  padding: 0 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .72);
  box-shadow: inset 0 0 0 1px rgba(222, 217, 210, .78);
  color: var(--ink);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.finish-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(44, 41, 40, .44);
  backdrop-filter: blur(6px);
}

.finish-overlay[hidden] {
  display: none;
}

.finish-dialog {
  width: min(100%, 330px);
  padding: 30px 24px 24px;
  border-radius: 28px;
  background: white;
  text-align: center;
  box-shadow: var(--shadow);
}

.finish-dialog h2 {
  margin: 0 0 12px;
  font-size: 30px;
}

.overrun {
  margin: 0 0 22px;
  font-size: 36px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--sand);
}

.ok-button {
  width: 100%;
}

@media (max-height: 720px) {
  .app {
    gap: 9px;
    grid-template-rows: auto auto auto auto auto auto;
  }

  .hourglass {
    width: min(52vw, 198px);
    max-height: 30vh;
    max-height: 30svh;
  }

  .clock {
    font-size: 44px;
  }

  .history {
    min-height: 58px;
  }
}

@media (max-width: 430px) {
  .app {
    padding: max(22px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
    gap: 8px;
  }

  .icon-button {
    width: 44px;
    height: 44px;
    font-size: 20px;
  }

  .palette-panel {
    gap: 6px;
    padding: 8px;
  }

  .hourglass {
    width: min(50vw, 190px);
    max-height: 28vh;
    max-height: 28svh;
  }

  .clock {
    font-size: clamp(40px, 12vw, 50px);
  }

  .time-editor {
    width: min(100%, 318px);
    gap: 10px;
  }

  .controls {
    gap: 9px;
  }

  .primary-button,
  .reset-button,
  .ok-button {
    min-height: 50px;
  }

  .history {
    min-height: 52px;
  }
}

@media (max-width: 390px), (max-height: 680px) {
  .app {
    padding: max(18px, env(safe-area-inset-top)) 16px max(8px, env(safe-area-inset-bottom));
    gap: 6px;
    grid-template-rows: auto auto auto auto auto auto;
  }

  h1 {
    font-size: 19px;
  }

  .icon-button {
    width: 40px;
    height: 40px;
    font-size: 19px;
  }

  .palette-panel {
    gap: 6px;
    padding: 8px;
    border-radius: 20px;
  }

  .swatch,
  .refresh-button {
    min-width: 32px;
    min-height: 32px;
    border-width: 3px;
    font-size: 13px;
  }

  .hourglass {
    width: min(44vw, 164px);
    max-height: 25vh;
    max-height: 25svh;
  }

  .clock {
    font-size: 38px;
  }

  .time-editor {
    width: min(100%, 318px);
    gap: 10px;
  }

  .digit-pair {
    gap: 5px;
  }

  .digit-column {
    gap: 4px;
  }

  .step-button {
    min-width: 30px;
    height: 30px;
    border-radius: 11px;
    font-size: 20px;
  }

  .digit-value {
    min-width: 30px;
    height: 42px;
    border-radius: 13px;
    font-size: 28px;
  }

  .unit-label {
    font-size: 15px;
  }

  .controls {
    gap: 9px;
  }

  .primary-button,
  .reset-button,
  .ok-button {
    min-height: 46px;
  }

  .history {
    min-height: 40px;
  }

  .history p {
    margin: 1px 0 6px;
    font-size: 13px;
  }

  .history-chip {
    min-width: 70px;
    min-height: 34px;
    padding: 0 11px;
    font-size: 14px;
  }
}

@media (max-height: 600px) {
  .app {
    grid-template-rows: auto auto auto auto auto auto;
    gap: 4px;
  }

  .hourglass {
    width: min(38vw, 142px);
    max-height: 22vh;
    max-height: 22svh;
  }

  .clock {
    font-size: 36px;
  }

  .history {
    min-height: 38px;
  }
}
