:root{
  --paper:#EEE5CF;
  --ink:#1E2840;
  --ink-55:rgba(30,40,64,.55);
  --ink-80:rgba(30,40,64,.82);
  --white:#F4F1E8;
  --yellow:#F2C230;
  --red:#C8312B;
  --orange:#E8742A;
  --blue:#1F5FB0;
  --green:#2E8B4F;
  --body:#151515;
  --serif:'PT Serif', Georgia, 'Times New Roman', serif;
  --sans:'PT Sans Narrow', 'Arial Narrow', Arial, sans-serif;
  --mono:'PT Mono', ui-monospace, 'Courier New', monospace;
  --rule:1.5px;
}

*{box-sizing:border-box;}

html,body{
  margin:0;padding:0;
  touch-action:manipulation;
  height:100%;
  overflow:hidden;
  overscroll-behavior:none;
}
body{
  background:var(--paper);
  color:var(--ink);
  font-family:var(--sans);
  -webkit-font-smoothing:antialiased;
  text-rendering:optimizeLegibility;
  -webkit-tap-highlight-color:transparent;
}

.grain{
  position:fixed; inset:0; z-index:60; pointer-events:none;
  opacity:.05; mix-blend-mode:multiply;
  background-image:url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='200'%20height='200'%3E%3Cfilter%20id='n'%3E%3CfeTurbulence%20type='fractalNoise'%20baseFrequency='0.9'%20numOctaves='2'%20stitchTiles='stitch'/%3E%3CfeColorMatrix%20type='saturate'%20values='0'/%3E%3C/filter%3E%3Crect%20width='200'%20height='200'%20filter='url(%23n)'/%3E%3C/svg%3E");
}

.svg-defs{position:absolute;width:0;height:0;overflow:hidden;}

/* ---------- layout ---------- */
.app{
  display:grid;
  grid-template-columns: minmax(140px, 196px) minmax(0,1fr) minmax(340px, 420px);
  align-items:stretch;
  height:100vh;
  height:100dvh;
  overflow:hidden;
  padding: 26px clamp(16px, 2.6vw, 44px) 26px;
}

.title-mobile{display:none;}

/* left leaflet margin */
.margin{
  padding-right: clamp(10px, 2vw, 30px);
  display:flex; flex-direction:column; gap:12px;
  padding-top:4px;
  min-height:0;
  overflow:hidden;
}
.caption{
  font-family:var(--sans);
  font-size:11px;
  line-height:1.38;
  letter-spacing:.07em;
  text-transform:uppercase;
  color:var(--ink);
  margin:0;
  max-width:22ch;
}
.caption.strong{font-weight:700;}
.caption.footnote{color:var(--ink-55); font-size:10px; margin-top:auto;}
.diagram{
  display:block;
  width:100%;
  mix-blend-mode:multiply;
  opacity:.92;
  user-select:none; -webkit-user-drag:none;
}
.d-grid{max-width:96px;}
.d-turn{max-width:118px; margin-top:6px;}
.d-stack{max-width:132px; margin-top:4px;}

/* ---------- stage ---------- */
.stage{
  -webkit-touch-callout:none; -webkit-user-select:none; user-select:none;
  position:relative;
  min-height:0;
  min-width:0;
  height:100%;
  touch-action:none;
  overscroll-behavior:none;
}
#cube{
  display:block;
  width:100%; height:100%;
  touch-action:none;
  cursor:grab;
}
.stage.dragging #cube{cursor:grabbing;}

.cube-shadow{
  position:absolute;
  left:50%; top:50%;
  width:46%; height:14%;
  transform:translate(-50%, 175%);
  background: radial-gradient(ellipse at center, rgba(30,40,64,.20) 0%, rgba(30,40,64,.13) 45%, rgba(30,40,64,0) 72%);
  pointer-events:none;
  z-index:0;
}

/* ---------- stamp ---------- */
.stamp-wrap{
  position:absolute; inset:0;
  z-index:5;
  pointer-events:none;
  opacity:0;
  transition:opacity .3s ease;
}
.stamp-wrap.show{opacity:1;}
.stamp{
  position:absolute;
  left:0; top:0;
  display:inline-block;
  transform:rotate(-8deg) scale(1.15);
  transform-origin:center;
  color:var(--red);
}
.stamp-inner{
  border:2.4px solid var(--red);
  border-radius:3px;
  padding:5px 9px;
  position:relative;
  filter:url(#rough);
}
.stamp-inner::after{
  content:"";
  position:absolute; inset:3px;
  border:1px solid var(--red);
  border-radius:1px;
}
.stamp-text{
  font-family:var(--sans);
  font-weight:700;
  font-size:26px;
  letter-spacing:.12em;
  line-height:1;
  padding:2px 6px;
  text-transform:uppercase;
}
.stamp-wrap.show .stamp{
  animation:stamp-press .25s ease-out forwards;
}
@keyframes stamp-press{
  from{opacity:0; transform:rotate(-8deg) scale(1.15);}
  to{opacity:.92; transform:rotate(-8deg) scale(1.0);}
}
.stamp-wrap.show .stamp{opacity:.92;}
.stamp-note{
  position:absolute;
  left:0; top:0;
  font-family:var(--sans);
  font-size:12.5px;
  letter-spacing:.06em;
  text-transform:uppercase;
  color:var(--ink);
  margin:0;
  max-width:200px;
  line-height:1.3;
}

/* ---------- right column ---------- */
.leaflet{
  border-left: var(--rule) solid var(--ink);
  padding-left: clamp(16px, 2vw, 26px);
  display:flex; flex-direction:column;
  padding-top:2px;
  min-width:0;
  min-height:0;
  overflow:hidden;
}
.title{
  font-family:var(--serif);
  font-style:italic;
  font-weight:700;
  font-size:clamp(30px, 3.4vw, 42px);
  line-height:1;
  margin:0 0 12px;
  letter-spacing:.01em;
  color:var(--ink);
}
.rule{
  border:0;
  border-top: var(--rule) solid var(--ink);
  margin:0 0 12px;
  width:100%;
}
.hint{
  font-family:var(--sans);
  font-size:12.5px;
  letter-spacing:.06em;
  text-transform:uppercase;
  line-height:1.45;
  color:var(--ink);
  margin:0 0 18px;
}

.moves{
  display:grid;
  grid-template-columns:repeat(6, minmax(0,1fr));
  gap:9px;
}
.move{
  -webkit-appearance:none; appearance:none;
  font:inherit; color:inherit;
  background:var(--paper);
  border: var(--rule) solid var(--ink);
  border-radius:2px;
  padding:9px 3px 8px;
  min-height:64px;
  display:flex; flex-direction:column; align-items:center; justify-content:center;
  gap:4px;
  cursor:pointer;
  transition:background .09s ease, color .09s ease;
}
.move .chip{
  width:18px; height:18px;
  border:1.5px solid var(--ink);
  border-radius:2px;
  display:block;
}
.move .mname{
  font-family:var(--sans);
  font-size:14px;
  font-weight:700;
  letter-spacing:.04em;
  text-transform:uppercase;
  line-height:1;
}
.move .mnot{
  font-family:var(--mono);
  font-size:12.5px;
  line-height:1;
  color:var(--ink-55);
}
.move:active, .move.pressed{
  background:var(--ink);
  color:var(--paper);
}
.move:active .mnot, .move.pressed .mnot{color:var(--paper);}
.move:active .chip, .move.pressed .chip{border-color:var(--paper);}
.move:focus-visible{outline:2px solid var(--ink); outline-offset:2px;}

.scramble{
  -webkit-appearance:none; appearance:none;
  font-family:var(--sans);
  font-weight:700;
  font-size:16px;
  letter-spacing:.09em;
  text-transform:uppercase;
  color:var(--paper);
  background:var(--red);
  border:0; border-radius:2px;
  width:100%;
  padding:14px 10px;
  margin-top:16px;
  min-height:50px;
  cursor:pointer;
  transition:opacity .12s ease;
}
.scramble:active{opacity:.86;}
.scramble:disabled{opacity:.55; cursor:default;}
.scramble:focus-visible{outline:2px solid var(--ink); outline-offset:2px;}

/* ---------- hint, timer, rating ---------- */
.actions{display:flex; gap:10px; margin-top:16px;}
.actions .scramble{margin-top:0; flex:1 1 auto;}
.hint-btn{
  -webkit-appearance:none; appearance:none;
  font-family:var(--sans); font-weight:700; font-size:16px;
  letter-spacing:.09em; text-transform:uppercase;
  color:var(--ink); background:var(--paper);
  border: var(--rule) solid var(--ink); border-radius:2px;
  padding:14px 18px; min-height:50px; cursor:pointer;
  flex:0 0 auto;
  transition:background .12s ease, color .12s ease, opacity .12s ease;
}
.hint-btn:active{background:var(--ink); color:var(--paper);}
.hint-btn:disabled{opacity:.55; cursor:default;}
.hint-btn:focus-visible{outline:2px solid var(--ink); outline-offset:2px;}
.hint-out{
  font-family:var(--mono); font-size:12.5px; letter-spacing:.02em;
  color:var(--ink); margin:9px 0 0; min-height:0; line-height:1.35;
  word-break:break-word;
}
.hint-out:empty{display:none;}
.hint-out .hint-move{font-weight:700; color:var(--red);}
.toolbar{
  display:flex; align-items:center; justify-content:space-between;
  gap:12px;
  margin-top:12px; padding-top:10px;
  border-top: var(--rule) solid var(--ink);
}
.records{display:flex; gap:16px; min-width:0;}
.record{display:flex; align-items:baseline; gap:6px;}
.record-k{
  font-family:var(--sans); font-weight:700; font-size:11px;
  letter-spacing:.09em; text-transform:uppercase; color:var(--ink-55);
}
.record-v{font-family:var(--mono); font-size:17px; line-height:1; color:var(--ink);}
.mini-actions{display:flex; gap:8px; flex:0 0 auto;}
.ghost-btn{
  -webkit-appearance:none; appearance:none;
  font-family:var(--sans); font-weight:700; font-size:12px;
  letter-spacing:.08em; text-transform:uppercase;
  color:var(--ink); background:transparent;
  border: var(--rule) solid var(--ink); border-radius:2px;
  padding:8px 11px; min-height:36px; cursor:pointer;
  white-space:nowrap;
  transition:background .12s ease, color .12s ease;
}
.ghost-btn:active{background:var(--ink); color:var(--paper);}
.ghost-btn:focus-visible{outline:2px solid var(--ink); outline-offset:2px;}

/* ---------- overlays: guide and rating ---------- */
body.overlay-open{overflow:hidden;}
.overlay{
  position:fixed; inset:0; z-index:80;
  display:flex; align-items:center; justify-content:center;
  padding:18px;
  background:rgba(30,40,64,.42);
}
.overlay[hidden]{display:none;}
.sheet{
  background:var(--paper);
  border: var(--rule) solid var(--ink);
  border-radius:3px;
  width:min(560px, 100%);
  max-height:100%;
  display:flex; flex-direction:column;
  box-shadow:0 18px 50px rgba(30,40,64,.30);
}
.sheet-head{
  display:flex; align-items:center; justify-content:space-between;
  gap:14px;
  padding:14px 16px 12px;
  border-bottom: var(--rule) solid var(--ink);
  flex:0 0 auto;
}
.sheet-title{
  font-family:var(--serif); font-style:italic; font-weight:700;
  font-size:clamp(20px, 2.4vw, 28px); line-height:1.05;
  margin:0;
}
.sheet-close{
  -webkit-appearance:none; appearance:none;
  font-family:var(--sans); font-weight:700; font-size:12px;
  letter-spacing:.08em; text-transform:uppercase;
  color:var(--paper); background:var(--ink);
  border:0; border-radius:2px;
  padding:9px 14px; min-height:38px; cursor:pointer;
  flex:0 0 auto;
}
.sheet-close:focus-visible{outline:2px solid var(--ink); outline-offset:2px;}
.sheet-body{
  padding:16px;
  overflow-y:auto;
  min-height:0;
  font-family:var(--sans);
  color:var(--ink);
}
.sheet-body p{margin:0 0 12px; font-size:14px; line-height:1.5; letter-spacing:.01em;}
.guide-h{
  font-family:var(--sans); font-weight:700; font-size:12px;
  letter-spacing:.09em; text-transform:uppercase;
  margin:18px 0 6px;
}
.guide-h:first-of-type{margin-top:6px;}
.guide-foot{color:var(--ink-55); font-size:12.5px;}
.name-row{display:flex; align-items:center; gap:10px; margin:0 0 6px;}
.name-k{font-family:var(--sans); font-weight:700; font-size:11.5px; letter-spacing:.09em; text-transform:uppercase; flex:0 0 auto;}
.name-input{
  flex:1 1 auto; min-width:0;
  font-family:var(--mono); font-size:16px; color:var(--ink);
  background:transparent; border:0; border-bottom: var(--rule) solid var(--ink);
  border-radius:0; padding:6px 2px;
}
.name-input:focus{outline:none; border-bottom-color:var(--red);}
.sheet-body p.name-note{font-size:12.5px; color:var(--ink-55); margin-bottom:14px;}
.rating-you{
  font-family:var(--mono); font-size:14px;
  border: var(--rule) solid var(--ink); border-radius:2px;
  padding:10px 12px; margin-bottom:14px;
}
.rating-sub{
  font-family:var(--sans); font-weight:700; font-size:11.5px;
  letter-spacing:.09em; text-transform:uppercase;
  margin:0 0 6px;
}
.rating-list{list-style:none; margin:0; padding:0;}
.rating-row{
  display:flex; gap:10px; align-items:baseline;
  font-family:var(--mono); font-size:14px; line-height:1.6;
  padding:5px 4px;
  border-bottom:1px solid rgba(30,40,64,.16);
}
.rating-rank{color:var(--ink-55); min-width:2.2em; flex:0 0 auto;}
.rating-name{
  color:var(--ink); text-decoration:none;
  overflow:hidden; text-overflow:ellipsis; white-space:nowrap;
}
a.rating-name:hover{text-decoration:underline;}
.rating-time{margin-left:auto; color:var(--ink); flex:0 0 auto;}
.rating-row.mine{
  background:rgba(200,49,43,.10);
  border-bottom-color:var(--red);
}
.rating-row.mine .rating-name, .rating-row.mine .rating-time{font-weight:700;}
.rating-empty{font-family:var(--sans); font-size:13px; color:var(--ink-55); padding:4px;}

/* numbered steps + move log: fills the leaflet's lower band */
.steps-log{
  margin-top:16px;
  padding-top:14px;
  border-top: var(--rule) solid var(--ink);
  flex:1 1 auto;
  min-height:0;
  overflow:hidden;
  display:flex;
  flex-direction:column;
  gap:12px;
}
.steps{flex:0 0 auto; overflow:hidden;}
.steps-title, .log-title{
  font-family:var(--sans);
  font-weight:700;
  font-size:11.5px;
  letter-spacing:.09em;
  text-transform:uppercase;
  color:var(--ink);
  margin:0 0 7px;
}
.steps ol{margin:0; padding-left:20px;}
.steps li{
  font-family:var(--sans);
  font-size:12.5px;
  line-height:1.42;
  letter-spacing:.02em;
  color:var(--ink);
  margin-bottom:5px;
  padding-left:2px;
}
.steps li::marker{color:var(--ink); font-weight:700;}
.log{
  flex:1 1 auto;
  min-height:0;
  display:flex;
  flex-direction:column;
  border: var(--rule) solid var(--ink);
  border-radius:2px;
  padding:11px 13px 10px;
}
.log-line{
  font-family:var(--mono);
  font-size:13.5px;
  line-height:1.6;
  letter-spacing:.02em;
  color:var(--ink);
  word-break:break-word;
  flex:1 1 auto;
  min-height:1.6em;
  overflow:hidden;
}
.log-stats{
  font-family:var(--mono);
  font-size:12px;
  letter-spacing:.02em;
  color:var(--ink-55);
  margin-top:8px;
  flex:0 0 auto;
  border-top:1px solid rgba(30,40,64,.28);
  padding-top:7px;
}

.bottom-marks{
  margin-top:16px;
  padding-top:16px;
  border-top: var(--rule) solid var(--ink);
  flex:0 0 auto;
}
.d-moves{width:100%; max-width:330px; margin-bottom:10px;}
.footer-line{color:var(--ink); font-size:11px;}

/* keep the leaflet intact on shorter laptop windows */
@media (max-height: 860px) and (min-width: 881px){
  .steps{display:none;}
}
@media (max-height: 820px) and (min-width: 881px){
  .caption.footnote{display:none;}
  .d-stack{max-width:110px;}
  .d-moves{max-width:280px;}
  .bottom-marks{padding-top:12px;}
}
@media (max-height: 760px) and (min-width: 881px){
  .d-stack{display:none;}
  .d-turn{max-width:96px;}
  .margin{gap:9px;}
}

/* ---------- mobile ---------- */
@media (max-width: 880px){
  .app{
    display:flex;
    flex-direction:column;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    height:100vh;
    height:100dvh;
    overflow:hidden;
  }
  .title-mobile{
    display:block;
    font-family:var(--serif);
    font-style:italic; font-weight:700;
    font-size:26px; line-height:1;
    margin:2px 0 8px;
    flex:0 0 auto;
  }
  .leaflet .title{display:none;}
  .margin{display:none;}
  .stage{
    order:2;
    flex:1 1 auto;
    min-height:0;
    height:auto;
  }
  .leaflet{
    order:3;
    flex:0 0 auto;
    border-left:0;
    border-top: var(--rule) solid var(--ink);
    padding-left:0;
    padding-top:12px;
    margin-top:12px;
    overflow:hidden;
  }
  .hint{margin-bottom:10px; font-size:12px;}
  .moves{grid-template-columns:repeat(4, minmax(0,1fr)); gap:7px;}
  .move{min-height:48px; padding:6px 3px 5px;}
  .move .mname{font-size:13px;}
  .move .mnot{font-size:11.5px;}
  .scramble{margin-top:12px; min-height:44px; padding:11px 10px; font-size:15px;}
  .steps{display:none;}
  .steps-log{
    margin-top:10px; padding-top:8px;
    flex:0 0 auto; gap:0;
  }
  .log{padding:9px 11px 8px;}
  .log-line{flex:0 0 auto; min-height:0; font-size:12.5px;}
  .log-stats{margin-top:5px; font-size:11.5px;}
  .bottom-marks{display:none;}
  .stamp-text{font-size:23px;}
  .stamp-note{font-size:11.5px; max-width:170px;}
}

@media (max-width: 400px){
  .leaflet{padding-top:12px;}
  .move .mname{font-size:11px;}
  .move .mnot{font-size:10px;}
}

/* landscape phones and short windows: keep controls beside the cube */
@media (max-height: 560px) and (min-width: 620px){
  .app{
    display:grid;
    grid-template-columns: minmax(0,1fr) minmax(250px,330px);
    align-items:stretch;
    height:100vh;
    height:100dvh;
    overflow:hidden;
    padding:10px 16px;
  }
  .title-mobile{display:none;}
  .margin{display:none;}
  .leaflet .title{display:block; font-size:24px; margin-bottom:8px;}
  .leaflet{overflow:hidden; border-left: var(--rule) solid var(--ink); border-top:0; padding-left:18px; padding-top:0; margin-top:0;}
  .stage{order:0; min-height:0; height:100%; flex:none;}
  .moves{grid-template-columns:repeat(4, minmax(0,1fr)); gap:6px;}
  .move{min-height:46px; padding:5px 2px 4px;}
  .move .chip{width:14px;height:14px;}
  .move .mname{font-size:11.5px;}
  .move .mnot{font-size:10.5px;}
  .bottom-marks{display:none;}
  .steps-log{display:none;}
  .scramble{margin-top:10px; min-height:42px; padding:9px; font-size:14px;}
  .hint{margin-bottom:10px; font-size:11px;}
}

/* ---------- hint, timer, rating: responsive ---------- */
@media (max-width: 880px){
  .actions{margin-top:12px; gap:8px;}
  .hint-btn{font-size:14px; min-height:46px; padding:11px 14px;}
  .hint-out{font-size:12px; margin-top:7px;}
  .toolbar{margin-top:10px; padding-top:8px; gap:10px;}
  .records{gap:14px;}
  .record-v{font-size:15px;}
  .record-k{font-size:10px;}
  .ghost-btn{font-size:11px; min-height:34px; padding:7px 9px;}
  .sheet-body p{font-size:13.5px;}
}
@media (max-height: 560px) and (min-width: 620px){
  .actions{margin-top:10px; gap:8px;}
  .hint-btn{font-size:13px; min-height:42px; padding:9px 12px;}
  .hint-out{font-size:11px; margin-top:6px;}
  .toolbar{margin-top:8px; padding-top:7px; gap:10px;}
  .record-v{font-size:14px;}
  .ghost-btn{font-size:10.5px; min-height:32px; padding:6px 8px;}
}
@media (max-height: 700px){
  .overlay{padding:10px; align-items:stretch;}
  .sheet{width:min(560px,100%);}
}

/* ---------- the solve clock: the one number to read at a glance ---------- */
.record-time{
  align-items:center; gap:8px;
  background:var(--ink); color:var(--paper);
  padding:6px 11px 6px 10px; border-radius:3px;
  box-shadow:0 2px 0 rgba(30,40,64,.25);
  transition:background-color .2s ease;
}
.record-time .record-k{color:rgba(238,229,207,.72);}
.record-time .record-v{
  color:#FFF8E6; font-weight:700; font-size:26px;
  font-variant-numeric:tabular-nums; letter-spacing:.01em;
  min-width:5.2ch; text-align:left;
}
.record-time.is-running{background:var(--red);}
.record-time.is-running .record-k{color:rgba(255,248,230,.85);}
@media (max-width: 880px){
  .record-time{padding:5px 9px;}
  .record-time .record-v{font-size:23px;}
}
@media (max-height: 560px) and (min-width: 620px){
  .record-time{padding:4px 8px;}
  .record-time .record-v{font-size:20px;}
}
.records{align-items:center;}
.record-v{white-space:nowrap;}
.record{flex:0 0 auto;}
@media (max-height: 520px){
  .record-time{padding:3px 8px;}
  .record-time .record-v{font-size:19px;}
  .toolbar{margin-top:6px; padding-top:6px;}
}

/* the solver's name sits where the clock is read */
.record-who{
  max-width:9em; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;
  text-transform:none; letter-spacing:.02em; font-size:12px;
}
@media (max-width: 880px){ .record-who{max-width:6.5em; font-size:11px;} }
@media (max-width: 380px){ .record-who{max-width:4.8em;} }

/* finish result: who solved it, then time and moves */
.stamp-note .result-who{
  display:block; font-weight:700; font-size:1.35em; letter-spacing:.03em;
  text-transform:none; color:var(--red); margin:2px 0 3px;
  overflow:hidden; text-overflow:ellipsis; white-space:nowrap;
}
.stamp-note .result-line{display:block; font-family:var(--mono); letter-spacing:.02em;}

/* all results list */
.sheet-body p.results-note{font-size:12.5px; color:var(--ink-55); margin:0 0 6px;}
.result-row .result-when{color:var(--ink-55); font-size:11.5px; flex:0 0 auto;}
.result-row .rating-time{font-size:13px;}
.results-more{margin:10px 0 0;}
.rating-sub-gap{margin-top:20px !important;}
@media (max-width: 420px){
  .result-row{flex-wrap:wrap; row-gap:0;}
  .result-row .rating-name{flex:1 1 0;}
  .result-row .rating-time{flex-basis:100%; text-align:right; margin-top:-4px;}
}
.name-row-gap{margin-top:20px; padding-top:14px; border-top:1px solid rgba(30,40,64,.16);}
