:root {
  --bg: #F8FAFD;
  --sf: #F0F4F9;
  --ink: #1F1F1F;
  --mute: #444746;
  --line: #C4C7C5;
  --ac: #0B57D0;
  --ok: #146C2E;
  --brk: #B06000;
  --sc: #D3E3FD;
  --onsc: #041E49;
  --err: #B3261E;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131314;
    --sf: #1E1F20;
    --ink: #E3E3E3;
    --mute: #9AA0A6;
    --line: #444746;
    --ac: #A8C7FA;
    --ok: #6DD58C;
    --brk: #FDD663;
    --sc: #0842A0;
    --onsc: #D3E3FD;
    --err: #F2B8B5;
    color-scheme: dark;
  }
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: "Vazirmatn", "Roboto", Tahoma, system-ui, sans-serif;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.serif {
  font-family: "Vazirmatn", "Roboto", sans-serif;
  font-weight: 500;
}

#app {
  height: 100svh;
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 8px;
  flex-shrink: 0;
}

header a.t {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
  letter-spacing: -0.5px;
}

nav {
  display: flex;
  gap: 6px;
}

nav a {
  color: var(--mute);
  text-decoration: none;
  font-size: .9rem;
  padding: 6px 18px;
  border-radius: 999px;
  transition: background-color .15s, color .15s;
}

nav a.on {
  background: var(--sc);
  color: var(--onsc);
  font-weight: 600;
}

.page {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

.page.on {
  display: flex;
}

/* Home Page: Zero-scroll fit */
#p-home.page.on {
  overflow: hidden;
  justify-content: space-between;
}

.mid {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  text-align: center;
  min-height: 0;
}

.date {
  color: var(--mute);
  font-size: .9rem;
}

.timer {
  font-size: clamp(2.8rem, 11vw, 3.8rem);
  font-weight: 400;
  direction: ltr;
  line-height: 1.25;
}

.state {
  color: var(--mute);
  min-height: 1.6em;
  font-size: 1rem;
}

.bottom {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 4px 0 max(16px, env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
}

.stage {
  position: relative;
  width: min(240px, 62vw, 31dvh);
  aspect-ratio: 1;
}

canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#hold {
  position: absolute;
  inset: 7%;
  border-radius: 50%;
  border: 0;
  background: transparent;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#hold:focus-visible {
  outline: 3px solid var(--ac);
  outline-offset: 6px;
}

.cap {
  color: var(--mute);
  font-size: .88rem;
  min-height: 1.5em;
}

.pill {
  background: var(--sc);
  color: var(--onsc);
  border: 0;
  border-radius: 999px;
  padding: 10px 24px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: transform .1s, opacity .15s;
}

.pill:active {
  transform: scale(.97);
}

.pill.brk {
  color: var(--brk);
}

.pill[hidden] {
  display: none !important;
}

/* Report & History Pages: Scrollable */
#p-report, #p-history {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 24px;
}

h2 {
  font-size: 1.2rem;
  margin: 10px 0 12px;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.stat {
  background: var(--sf);
  border-radius: 16px;
  padding: 14px 16px;
}

.stat span {
  display: block;
  color: var(--mute);
  font-size: .82rem;
}

.stat strong {
  font-size: 1.6rem;
  font-weight: 700;
}

.bars {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  height: 150px;
  background: var(--sf);
  border-radius: 16px;
  padding: 14px 10px 8px;
  margin-bottom: 16px;
}

.bar {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  gap: 4px;
  font-size: .72rem;
  color: var(--mute);
}

.bar i {
  display: block;
  width: 100%;
  max-width: 30px;
  background: var(--ac);
  border-radius: 6px 6px 2px 2px;
  opacity: .55;
  transition: height .2s ease;
}

.bar.today i {
  opacity: 1;
}

.bar.today {
  color: var(--ink);
  font-weight: 600;
}

.bars.m {
  gap: 2px;
  padding: 14px 6px 8px;
}

.bars.m .bar {
  font-size: .58rem;
  cursor: pointer;
  gap: 2px;
}

.bars.m .bar span {
  min-height: 1em;
  white-space: nowrap;
}

.bars.m .bar i {
  max-width: none;
  border-radius: 3px 3px 1px 1px;
}

.bar.fri {
  color: var(--ac);
}

.bar.sel {
  background: var(--sc);
  border-radius: 6px;
}

.dline {
  margin: -4px 0 16px;
  color: var(--mute);
  font-size: .9rem;
  min-height: 1.7em;
  text-align: center;
}

.acts {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  padding-bottom: 12px;
}

.acts .pill {
  flex: 1 1 180px;
  text-align: center;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0 0 24px;
  display: grid;
  gap: 8px;
}

li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: var(--sf);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: .92rem;
}

li .m {
  color: var(--mute);
  font-size: .8rem;
}

li button {
  background: none;
  border: 0;
  color: var(--ac);
  cursor: pointer;
  font: inherit;
  font-size: .82rem;
}

.empty {
  color: var(--mute);
  text-align: center;
  padding: 32px 16px;
}

.mon {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.mon .pill {
  padding: 6px 16px;
  font-size: 1.1rem;
}

.pill:disabled {
  opacity: .35;
  cursor: default;
}

li.dh {
  background: none;
  padding: 14px 4px 2px;
  color: var(--mute);
  font-size: .85rem;
}

li.dh strong {
  color: var(--ink);
}

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

.pill.fill {
  background: var(--ac);
  color: var(--bg);
}

.pill.gh {
  background: none;
  color: var(--ac);
}

.pill.del {
  background: none;
  color: var(--err);
}

/* Dialog Modal */
dialog {
  border: 0;
  border-radius: 28px;
  background: var(--sf);
  color: var(--ink);
  padding: 24px;
  width: min(380px, 92vw);
  font-family: inherit;
  box-shadow: 0 8px 32px rgba(0,0,0,.3);
}

dialog::backdrop {
  background: rgba(0,0,0,.5);
  backdrop-filter: blur(2px);
}

dialog h3 {
  margin: 0 0 8px;
  font-size: 1.25rem;
}

dialog label {
  display: block;
  margin-top: 14px;
  font-size: .85rem;
  color: var(--mute);
}

dialog input {
  display: block;
  width: 100%;
  margin-top: 5px;
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font: inherit;
}

dialog input:focus {
  outline: none;
  border-color: var(--ac);
  box-shadow: 0 0 0 2px var(--sc);
}

.err {
  color: var(--err);
  min-height: 1.6em;
  font-size: .85rem;
  margin-top: 8px;
}

.dact {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 12px;
}

/* Toast Notification / Snackbar */
.toast {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 20px;
  display: flex;
  gap: 16px;
  align-items: center;
  border-radius: 12px;
  font-size: .9rem;
  opacity: 0;
  transition: opacity .25s, transform .25s;
  pointer-events: none;
  z-index: 1000;
  max-width: 90vw;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}

.toast.show {
  opacity: 1;
  pointer-events: auto;
}

.toast button {
  background: none;
  border: 0;
  font: inherit;
  font-weight: 600;
  color: var(--ac);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}

@media (prefers-color-scheme: light) {
  .toast button {
    color: #A8C7FA;
  }
}

@media (prefers-color-scheme: dark) {
  .toast button {
    color: #A8C7FA;
  }
}

.toast button[hidden] {
  display: none;
}

/* Loading Overlay */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 16px;
  z-index: 900;
  transition: opacity .25s ease;
}

.loading-overlay[hidden] {
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
}

.spinner {
  width: 44px;
  height: 44px;
  border: 3.5px solid var(--sc);
  border-top-color: var(--ac);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  font-size: .95rem;
  color: var(--mute);
}

/* Login Screen Overlay */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  z-index: 950;
}

.login-overlay[hidden] {
  display: none !important;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--sf);
  border-radius: 28px;
  padding: 32px 24px 28px;
  box-shadow: 0 10px 30px rgba(0,0,0,.15);
  text-align: center;
}

.login-logo {
  border-radius: 18px;
  margin-bottom: 8px;
}

.login-card h1 {
  font-size: 1.5rem;
  margin: 6px 0 4px;
}

.login-desc {
  font-size: .85rem;
  color: var(--mute);
  margin: 0 0 20px;
}

.field {
  text-align: right;
  margin-bottom: 12px;
}

.field label {
  display: block;
  font-size: .85rem;
  color: var(--mute);
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 16px;
  font: inherit;
  font-size: 1.05rem;
  text-align: center;
  letter-spacing: 4px;
}

.field input:focus {
  outline: none;
  border-color: var(--ac);
  box-shadow: 0 0 0 2px var(--sc);
}

.login-btn {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
  margin-top: 8px;
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
  .toast, .pill, .spinner, .bar i {
    transition: none !important;
    animation: none !important;
  }
}
