:root{
  --bg: #000;
  --fg: #fff;
  --muted: rgba(255,255,255,.68);
  --border: rgba(255,255,255,.22);
  --border-strong: rgba(255,255,255,.5);
  --arrow-hover-bg: rgba(255,255,255,.08);
  --fc-bg-top: #252525;
  --fc-bg-bot: #2b2b2b;
  --fc-fg-top: #f0f0f0;
  --fc-fg-bot: rgba(240,240,240,.4);
  --fc-font: 'Nunito', ui-rounded, 'Helvetica Neue', sans-serif;
  --flip-gap-color: #000;
  --flip-shadow: 0 6px 28px rgba(0,0,0,.75);
  --timer-font-size: 14vw;
  --theme-transition: background-color .35s ease, color .35s ease, border-color .35s ease;
}

html[data-theme="light"]{
  --bg: #f4f3ef;
  --fg: #111;
  --muted: rgba(0,0,0,.5);
  --border: rgba(0,0,0,.15);
  --border-strong: rgba(0,0,0,.35);
  --arrow-hover-bg: rgba(0,0,0,.06);
  --fc-bg-top: #252525;
  --fc-bg-bot: #2b2b2b;
  --fc-fg-top: #f0f0f0;
  --fc-fg-bot: rgba(240,240,240,.4);
  --fc-font: 'Nunito', ui-rounded, 'Helvetica Neue', sans-serif;
  --flip-gap-color: #f4f3ef;
  --flip-shadow: 0 6px 28px rgba(0,0,0,.55);
}

*{ box-sizing:border-box; }

body{
  min-height:100svh;
  margin:0;
  padding:56px 16px 24px;
  background:var(--bg);
  color:var(--fg);
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
  font-feature-settings:"tnum" 1;
  overflow-x:hidden;
  transition: var(--theme-transition);
}

body.timesup{
  animation:bgflash 1s steps(1,end) infinite;
}
@keyframes bgflash{
  0%,49%{ background: var(--bg); }
  50%,100%{ background: #ff0000; }
}

#small-clock{
  position:fixed;
  top:14px;
  left:50%;
  transform:translateX(-50%);
  font-size:.95rem;
  color:var(--muted);
  letter-spacing:.02em;
  white-space:nowrap;
  user-select:none;
  pointer-events:none;
  z-index:5;
  transition: color .35s ease;
}

/* ---- Top-right control cluster ---- */
#top-controls{
  position:fixed;
  top:12px;
  right:16px;
  display:flex;
  gap:8px;
  z-index:10;
}

.ctrl-btn{
  width:36px;
  height:36px;
  border-radius:50%;
  border:1px solid var(--border);
  background:transparent;
  color:var(--muted);
  cursor:pointer;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:0;
  position:relative;
  transition: var(--theme-transition), transform .06s;
  font-size:.95rem;
  line-height:1;
}
.ctrl-btn:hover{
  background:var(--fg);
  color:var(--bg);
  border-color:var(--fg);
}
.ctrl-btn:active{ transform:translateY(1px) scale(.94); }
.ctrl-btn:focus-visible{
  outline:2px solid var(--fg);
  outline-offset:3px;
}

/* Icon crossfade */
.ctrl-icon{
  position:absolute;
  width:17px;
  height:17px;
  transition:opacity .3s ease, transform .3s ease;
  pointer-events:none;
  display:flex;
  align-items:center;
  justify-content:center;
}

/* Theme toggle */
.icon-moon{ opacity:1; transform:scale(1) rotate(0deg); }
.icon-sun { opacity:0; transform:scale(.7) rotate(-30deg); }
html[data-theme="light"] .icon-moon{ opacity:0; transform:scale(.7) rotate(30deg); }
html[data-theme="light"] .icon-sun { opacity:1; transform:scale(1) rotate(0deg); }

/* Display mode toggle */
.icon-to-flip   { opacity:1; transform:scale(1); }
.icon-to-digital{ opacity:0; transform:scale(.7); }
body.flip-mode .icon-to-flip   { opacity:0; transform:scale(.7); }
body.flip-mode .icon-to-digital{ opacity:1; transform:scale(1); }

/* ---- Layout ---- */
.container{
  width:100%;
  text-align:center;
}

#quick-links{
  display:flex;
  gap:10px;
  justify-content:center;
  flex-wrap:wrap;
  margin-bottom:14px;
}

button{
  padding:10px 16px;
  font-size:.95rem;
  background:transparent;
  border:1px solid var(--border-strong);
  border-radius:var(--radius);
  color:var(--fg);
  cursor:pointer;
  transition: background-color .15s, color .15s, border-color .15s, transform .06s, opacity .15s;
  line-height:1;
}
button:hover{ background:var(--fg); color:var(--bg); }
button:active{ transform:translateY(1px); }
button:focus-visible{ outline:2px solid var(--fg); outline-offset:3px; }

#quick-links button{
  border-color:var(--border);
  color:var(--muted);
}
#quick-links button:hover{
  border-color:var(--fg);
  color:var(--bg);
}

.timer-wrap{
  width:100vw;
  margin-left:calc(50% - 50vw);
  margin-right:calc(50% - 50vw);
  display:flex;
  justify-content:center;
  align-items:center;
}

#countdown{
  display:flex;
  align-items:center;
  justify-content:center;
  gap:18px;
  margin:18px 0 22px;
  font-weight:300;
  font-variant-numeric:tabular-nums;
  letter-spacing:.01em;
  user-select:none;
  line-height:1;
  font-size:var(--timer-font-size);
  max-width:100%;
  flex-wrap:nowrap;
}

.unit{
  display:flex;
  flex-direction:column;
  align-items:center;
  gap:10px;
  min-width:2.2ch;
}

.value{
  min-width:2.2ch;
  display:inline-block;
}

.sep{
  opacity:.45;
  transform:translateY(-.04em);
}

.arrow{
  width:44px;
  height:32px;
  padding:0;
  border-radius:8px;
  border:1px solid transparent;
  background:transparent;
  color:var(--fg);
  font-size:1.1rem;
  opacity:.55;
}
.arrow:hover{
  opacity:1;
  border-color:var(--border);
  background:var(--arrow-hover-bg);
  color:var(--fg);
}

#actions{
  display:flex;
  gap:10px;
  justify-content:center;
  flex-wrap:wrap;
  margin-top:10px;
}

.hidden{ display:none !important; }
.visually-hidden{
  position:absolute !important;
  width:1px;height:1px;
  padding:0;margin:-1px;
  overflow:hidden;clip:rect(0 0 0 0);
  white-space:nowrap;border:0;
}

.utility{
  position:fixed;
  bottom:44px;
  left:50%;
  transform:translateX(-50%);
  width:min(560px,92vw);
  display:flex;
  align-items:center;
  gap:12px;
  color:var(--muted);
  font-size:.9rem;
  user-select:none;
  z-index:5;
  transition: color .35s ease;
}
#font-slider{ flex:1; accent-color:var(--fg); }

#target-row{
  display:flex;
  gap:10px;
  justify-content:center;
  flex-wrap:wrap;
  margin:8px 0 14px;
  color:var(--muted);
  align-items:center;
}
#target-row label{ color:var(--muted); font-size:.95rem; }

select{
  padding:10px 12px;
  font-size:.95rem;
  background:var(--bg);
  border:1px solid var(--border);
  border-radius:var(--radius);
  color:var(--fg);
  outline:none;
  transition:var(--theme-transition);
}
select option{ background:var(--bg); color:var(--fg); }
select:focus-visible{ outline:2px solid var(--fg); outline-offset:3px; }

/* ============================================
   FLIP CLOCK
============================================ */

.flip-wrap{
  display:none;
  gap:5px;
}

body.flip-mode .value{ display:none; }
body.flip-mode .flip-wrap{ display:flex; }

.flip-card{
  position:relative;
  width:0.72em;
  height:1.14em;
  perspective:600px;
  box-shadow: var(--flip-shadow);
  border-radius:0.13em;
}

.fc-top,
.fc-bottom,
.fc-fold{
  position:absolute;
  left:0; right:0;
  overflow:hidden;
  display:flex;
  justify-content:center;
}

.fc-top{
  top:0;
  height:calc(50% - 1.5px);
  border-radius:0.13em 0.13em 0 0;
  background:var(--fc-bg-top);
  align-items:flex-start;
}

.fc-bottom{
  bottom:0;
  height:calc(50% - 1.5px);
  border-radius:0 0 0.13em 0.13em;
  background:var(--fc-bg-bot);
  align-items:flex-end;
}

.fc-bottom::before{
  content:'';
  position:absolute;
  inset:0;
  background:linear-gradient(to bottom, transparent 20%, rgba(0,0,0,.18));
  z-index:1;
  pointer-events:none;
  border-radius:0 0 0.13em 0.13em;
}

.fc-fold{
  top:0;
  height:calc(50% - 1.5px);
  border-radius:0.13em 0.13em 0 0;
  background:var(--fc-bg-top);
  align-items:flex-start;
  transform-origin:50% 100%;
  z-index:5;
  box-shadow:0 4px 10px rgba(0,0,0,.5);
}

.fc-fold::after{
  content:'';
  position:absolute;
  bottom:0; left:0; right:0;
  height:30%;
  background:linear-gradient(to top, rgba(0,0,0,.3), transparent);
  pointer-events:none;
}

.fc-fold-bottom{
  position:absolute;
  bottom:0; left:0; right:0;
  height:calc(50% - 1.5px);
  border-radius:0 0 0.13em 0.13em;
  background:var(--fc-bg-bot);
  overflow:hidden;
  display:flex;
  justify-content:center;
  align-items:flex-end;
  transform-origin:50% 0%;
  transform:rotateX(-90deg);
  z-index:4;
  box-shadow:0 -2px 6px rgba(0,0,0,.4);
}

.fc-fold-bottom::before{
  content:'';
  position:absolute;
  inset:0;
  background:linear-gradient(to bottom, transparent 20%, rgba(0,0,0,.18));
  z-index:1;
  pointer-events:none;
  border-radius:0 0 0.13em 0.13em;
}

.fc-inner{
  height:200%;
  width:100%;
  display:flex;
  align-items:center;
  justify-content:center;
  font-family:var(--fc-font);
  font-weight:700;
  font-variant-numeric:tabular-nums;
  line-height:1;
  font-size:1em;
  letter-spacing:0em;
  pointer-events:none;
}

.fc-top .fc-inner,
.fc-fold .fc-inner{
  color:var(--fc-fg-top);
}

.fc-bottom .fc-inner,
.fc-fold-bottom .fc-inner{
  color:var(--fc-fg-bot);
}

.flip-card::after{
  content:'';
  position:absolute;
  top:calc(50% - 1.5px);
  left:0; right:0;
  height:3px;
  background:var(--flip-gap-color);
  z-index:9;
  transition:background .35s ease;
}

@keyframes fold-down{
  0%  { transform:rotateX(0deg); }
  100%{ transform:rotateX(-90deg); }
}
.fc-fold.folding{
  animation:fold-down .28s cubic-bezier(.4,0,.6,1) forwards;
}

@keyframes fold-bottom-in{
  0%  { transform:rotateX(-90deg); }
  100%{ transform:rotateX(0deg); }
}
.fc-fold-bottom.folding-bottom{
  animation:fold-bottom-in .28s cubic-bezier(.4,0,.6,1) .2s both;
}

body.flip-mode .sep{
  transform:none;
  line-height:1.08;
  opacity:.45;
}

/* ============================================
   RESPONSIVE + ACCESSIBILITY
============================================ */
@media (max-width:480px){
  body{ padding:18px 12px; }
  .utility{ bottom:40px; }
  #countdown{ gap:14px; }
}

@media (prefers-reduced-motion:reduce){
  body.timesup{ animation:none; background:#7a0000; }
  body, #small-clock, .utility, select, .ctrl-btn{ transition:none; }
  .ctrl-icon{ transition:none; }
  .fc-fold.folding{ animation:none; }
  .fc-fold-bottom.folding-bottom{ animation:none; }
}
