:root{
  --bg:#0b0806;
  --panel:rgba(26,20,15,0.92);
  --panel-2:#241b14;
  --panel-3:#2e231a;
  --border:rgba(255,220,160,0.12);
  --accent:#e2b755;
  --accent-2:#f5d27f;
  --text:#f4ede2;
  --muted:#a89886;
  --good:#5bd08a;
  --radius:14px;
}
*{box-sizing:border-box;}
html, body{height:100%;}
body{
  margin:0;
  height:100dvh;
  overflow:hidden;
  background:#0b0806;
  color:var(--text);
  font-family:Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  display:flex;
  flex-direction:column;
}
button{cursor:pointer; font-family:inherit; color:inherit;}
[hidden]{display:none !important;}

/* ---------- top bar ---------- */
.topbar{
  flex:0 0 auto;
  display:flex; align-items:center; justify-content:space-between;
  padding:8px 18px;
  background:linear-gradient(180deg, #17110c, #0d0906);
  border-bottom:1px solid var(--border);
}
.topbar .back{color:var(--muted); text-decoration:none; font-size:0.88rem; min-width:110px;}
.topbar .back:hover{color:var(--text);}
.topbar h1{margin:0; font:700 1.15rem Cinzel, Georgia, serif; letter-spacing:0.06em; display:flex; align-items:center; gap:8px;}
.topbar h1 .crest{color:var(--accent); font-size:1.3rem;}
.topbar h1 .thin{font-weight:600; color:var(--accent-2);}
.top-actions{display:flex; gap:6px; min-width:110px; justify-content:flex-end;}
.icon-btn{
  width:36px; height:34px; border-radius:10px;
  border:1px solid var(--border); background:var(--panel-2); font-size:1rem;
}
.icon-btn:hover{border-color:var(--accent);}

/* ---------- layout ---------- */
.layout{flex:1 1 auto; min-height:0; display:flex; gap:14px; padding:12px 14px;}
.board-wrap{flex:1 1 auto; min-width:0; min-height:0; display:flex;}
.canvas-frame{flex:1 1 auto; position:relative; min-height:0; border-radius:16px; overflow:hidden; box-shadow:0 24px 60px rgba(0,0,0,0.6), 0 0 0 1px var(--border);}
#boardCanvas{position:absolute; inset:0; width:100%; height:100%; display:block; touch-action:none; cursor:pointer;}

.loading{
  position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:14px;
  background:radial-gradient(circle at 50% 45%, #2a1c12, #0b0806 70%); color:var(--muted); font-size:0.9rem;
  transition:opacity 0.5s;
}
.loading.done{opacity:0; pointer-events:none;}
.spinner{width:38px; height:38px; border-radius:50%; border:3px solid rgba(226,183,85,0.2); border-top-color:var(--accent); animation:spin 0.9s linear infinite;}
@keyframes spin{to{transform:rotate(360deg);}}

.view-tools{
  position:absolute; left:12px; bottom:12px; display:flex; align-items:center; gap:6px;
  padding:6px; border-radius:12px; background:rgba(14,10,7,0.7); backdrop-filter:blur(8px); -webkit-backdrop-filter:blur(8px);
  border:1px solid var(--border);
}
.view-tools button{width:34px; height:32px; border-radius:8px; border:1px solid var(--border); background:var(--panel-2); font-size:1.05rem;}
.view-tools button:hover{border-color:var(--accent);}
.view-tools .tip{font-size:0.72rem; color:var(--muted); padding:0 6px;}

.banner{
  position:absolute; left:50%; top:42%; transform:translate(-50%,-50%);
  padding:14px 30px; border-radius:16px;
  background:linear-gradient(180deg, rgba(44,30,14,0.95), rgba(14,10,4,0.95));
  border:1px solid rgba(240,200,110,0.6); box-shadow:0 12px 40px rgba(0,0,0,0.6);
  color:#ffe29a; font:700 1.6rem Cinzel, Georgia, serif; letter-spacing:0.05em; text-align:center;
  pointer-events:none; animation:pop 0.35s ease-out;
}
.banner small{display:block; font:500 0.85rem Inter, sans-serif; color:#e9d9b8; margin-top:4px; letter-spacing:0;}
@keyframes pop{from{transform:translate(-50%,-50%) scale(0.6); opacity:0;} to{transform:translate(-50%,-50%) scale(1); opacity:1;}}

/* ---------- side panel ---------- */
.panel{
  flex:0 0 330px; width:330px; max-height:100%; overflow-y:auto;
  background:var(--panel); border:1px solid var(--border); border-radius:18px;
  box-shadow:0 18px 50px rgba(0,0,0,0.5);
  padding:16px; display:flex; flex-direction:column; gap:12px;
}
.panel-head h2{margin:0 0 4px; font:700 1.2rem Cinzel, Georgia, serif; letter-spacing:0.04em;}
.cast{margin:0; font-size:0.8rem; color:var(--muted); line-height:1.45;}
.modes{display:grid; grid-template-columns:1fr 1fr; gap:8px;}
.mode{
  display:flex; flex-direction:column; align-items:flex-start; gap:2px; text-align:left;
  padding:11px 12px; border-radius:12px; border:1px solid var(--border);
  background:linear-gradient(180deg, var(--panel-3), var(--panel-2));
  transition:border-color 0.15s, transform 0.15s;
}
.mode:hover{transform:translateY(-1px); border-color:rgba(226,183,85,0.5);}
.mode .mi{font-size:1.3rem;}
.mode b{font-size:0.88rem;}
.mode small{font-size:0.72rem; color:var(--muted);}
.mode.active{border-color:var(--accent); box-shadow:0 0 0 1px var(--accent) inset, 0 6px 18px rgba(226,183,85,0.15);}
.opts{display:flex; flex-direction:column;}
.opts label, .settings label{font-size:0.76rem; color:var(--muted); margin:10px 0 6px; text-transform:uppercase; letter-spacing:0.06em;}
.seg{display:flex; gap:4px; padding:4px; background:#15100c; border-radius:11px; border:1px solid var(--border);}
.seg button{flex:1; padding:8px 4px; border-radius:8px; border:none; background:transparent; font-size:0.8rem; color:var(--muted);}
.seg button.active{background:linear-gradient(180deg, #3a2c1d, #2a1f15); color:var(--accent-2); box-shadow:0 1px 0 rgba(255,255,255,0.06) inset, 0 2px 6px rgba(0,0,0,0.4);}
input[type="text"]{
  width:100%; padding:10px 12px; border-radius:10px;
  border:1px solid var(--border); background:#15100c; color:var(--text); font-size:0.95rem;
}
input[type="text"]:focus{outline:none; border-color:var(--accent);}
.primary-btn{
  width:100%; padding:12px; margin-top:14px; border-radius:11px; border:none;
  background:linear-gradient(180deg, #f5cf6c, #c98a1b);
  box-shadow:inset 0 1px 0 rgba(255,255,255,0.35), 0 6px 16px rgba(201,138,27,0.3);
  color:#2a1a05; font-weight:700; font-size:0.95rem; letter-spacing:0.02em;
}
.primary-btn:hover{filter:brightness(1.06);}
.primary-btn:disabled{opacity:0.5; cursor:default;}
.secondary-btn{
  width:100%; padding:10px; margin-top:8px; border-radius:11px;
  border:1px solid var(--border); background:transparent; font-size:0.86rem;
}
.secondary-btn:hover{border-color:rgba(226,183,85,0.5);}
.hint{font-size:0.8rem; color:var(--muted); margin:8px 0 0; line-height:1.45;}
.room-code-box{margin-top:12px; padding:12px; background:#15100c; border:1px dashed rgba(226,183,85,0.4); border-radius:12px; text-align:center;}
.room-code-box .code{font:700 1.7rem Cinzel, Georgia, serif; letter-spacing:6px; color:var(--accent);}
.room-code-box .sub{font-size:0.75rem; color:var(--muted);}
.link-row{display:flex; gap:6px; margin-top:8px;}
.link-row input{flex:1; font-size:0.72rem; padding:8px;}
.link-row button{padding:0 12px; border-radius:9px; border:1px solid var(--border); background:var(--panel-2); color:var(--accent);}

/* ---------- in-game ---------- */
.player{
  display:flex; align-items:center; gap:10px;
  padding:10px; border-radius:14px;
  background:linear-gradient(180deg, #1a130e, #110c08);
  border:1px solid var(--border);
  transition:border-color 0.25s, box-shadow 0.25s;
}
.player.turn{border-color:var(--accent); box-shadow:0 0 0 1px var(--accent) inset, 0 0 22px rgba(226,183,85,0.18);}
.avatar{flex:0 0 48px; width:48px; height:48px; border-radius:12px; background:radial-gradient(circle at 50% 35%, #3a2b1d, #140e09); display:flex; align-items:center; justify-content:center; overflow:hidden; border:1px solid var(--border);}
.pinfo{flex:1; min-width:0;}
.pname{font-weight:600; font-size:0.95rem; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;}
.captured{display:flex; flex-wrap:wrap; align-items:center; min-height:26px; margin-top:2px;}
.captured canvas{margin-right:-7px;}
.captured .adv{margin-left:10px; font-size:0.78rem; font-weight:600; color:var(--good);}
.thinking{display:flex; gap:3px; padding-right:4px;}
.thinking i{width:6px; height:6px; border-radius:50%; background:var(--accent); animation:blink 1s infinite;}
.thinking i:nth-child(2){animation-delay:0.15s;} .thinking i:nth-child(3){animation-delay:0.3s;}
@keyframes blink{0%,100%{opacity:0.2;} 50%{opacity:1;}}
.status{text-align:center; padding:9px; border-radius:10px; background:#15100c; font-size:0.9rem; font-weight:600; border:1px solid var(--border);}
.status.check{color:#ff8a78; border-color:rgba(255,90,70,0.5);}
.moves{
  flex:1 1 auto; min-height:90px; max-height:260px; overflow-y:auto;
  background:#15100c; border-radius:12px; border:1px solid var(--border);
  padding:6px; font:0.84rem ui-monospace, "SF Mono", Menlo, monospace;
  display:grid; grid-template-columns:36px 1fr 1fr; align-content:start;
}
.moves span{padding:3px 6px; border-radius:6px;}
.moves .n{color:var(--muted); padding-left:4px;}
.moves span:nth-child(6n+1), .moves span:nth-child(6n+2), .moves span:nth-child(6n+3){background:rgba(255,255,255,0.025);}
.moves .last{color:#1d1405; background:var(--accent) !important; font-weight:700;}
.moves .empty{grid-column:1 / -1; color:var(--muted); font-family:Inter, sans-serif; font-size:0.8rem; text-align:center; padding:14px;}
.actions{display:grid; grid-template-columns:repeat(4, 1fr); gap:6px;}
.actions button{
  display:flex; flex-direction:column; align-items:center; gap:2px;
  padding:8px 2px; border-radius:11px; border:1px solid var(--border);
  background:linear-gradient(180deg, var(--panel-3), var(--panel-2)); font-size:0.74rem;
}
.actions button span{font-size:1.05rem;}
.actions button:hover:not(:disabled){border-color:rgba(226,183,85,0.55);}
.actions button:disabled{opacity:0.35; cursor:default;}

/* ---------- modals ---------- */
.modal{position:fixed; inset:0; background:rgba(0,0,0,0.6); backdrop-filter:blur(3px); -webkit-backdrop-filter:blur(3px); display:flex; align-items:center; justify-content:center; z-index:20; padding:16px;}
.modal-box{
  width:min(380px, 100%);
  background:linear-gradient(180deg, #2a1f16, #16100b); border:1px solid rgba(226,183,85,0.5); border-radius:18px;
  padding:20px; text-align:center; box-shadow:0 24px 70px rgba(0,0,0,0.7); animation:rise 0.25s ease-out;
}
@keyframes rise{from{transform:translateY(12px); opacity:0;} to{transform:none; opacity:1;}}
.modal-box h3{margin:0 0 6px; font:700 1.25rem Cinzel, Georgia, serif; letter-spacing:0.04em;}
.modal-box p{margin:0 0 12px; color:var(--muted);}
.modal-box.settings{text-align:left;}
.over .trophy{font-size:2.6rem; margin-bottom:4px;}
.modal-actions{display:flex; flex-direction:column;}
.promo-options{display:flex; gap:8px; justify-content:center;}
.promo-options button{
  display:flex; flex-direction:column; align-items:center; gap:4px;
  padding:8px; border-radius:12px; border:1px solid var(--border); background:var(--panel-2); font-size:0.76rem;
}
.promo-options button:hover{border-color:var(--accent);}

@media (max-width: 880px), (max-aspect-ratio: 1/1){
  .topbar{padding:8px 10px;}
  .topbar .back, .top-actions{min-width:0;}
  .topbar h1 .thin{display:none;}
  .layout{flex-direction:column; padding:8px; gap:8px;}
  .board-wrap{flex:0 0 auto; height:min(60dvh, 105vw);}
  .panel{flex:1 1 auto; width:100%; padding:12px; gap:8px; border-radius:14px;}
  .moves{min-height:60px; max-height:110px;}
  .view-tools .tip{display:none;}
}

/* ---------- phones in landscape: board left at full height, panel right ---------- */
@media (orientation: landscape) and (max-height: 600px){
  .topbar{padding:4px 10px;}
  .topbar h1{font-size:0.95rem;}
  .icon-btn{width:30px; height:28px; font-size:0.85rem;}
  .layout{flex-direction:row; padding:6px; gap:8px;}
  .board-wrap{flex:1 1 auto; height:auto;}
  .panel{flex:0 0 min(300px, 40vw); width:min(300px, 40vw); align-self:stretch; max-height:100%; padding:8px 10px; gap:6px; border-radius:12px;}
  .panel-head h2{font-size:1rem;} .cast{display:none;}
  .modes{gap:5px;} .mode{padding:6px 8px;} .mode small{display:none;} .mode .mi{font-size:1rem;}
  .player{padding:5px 8px; gap:8px;} .avatar{flex-basis:34px; width:34px; height:34px;}
  .captured{min-height:18px;}
  .status{padding:5px; font-size:0.8rem;}
  .moves{min-height:40px; max-height:none; flex:1 1 0;}
  .actions button{padding:4px 2px; font-size:0.66rem;} .actions button span{font-size:0.9rem;}
  .secondary-btn{padding:6px; margin-top:4px;}
  .view-tools .tip{display:none;}
}

/* ---------- close-ups: look mode, focus card, character gallery ---------- */
.view-tools .wide-btn{width:auto; padding:0 10px; font-size:0.8rem; font-weight:600;}
.view-tools .wide-btn.on{background:var(--accent); color:#1d1405; border-color:transparent;}
.focus-card{
  position:absolute; right:12px; bottom:12px; width:min(300px, calc(100% - 24px)); padding:12px 14px;
  border-radius:14px; background:rgba(14,10,7,0.85); backdrop-filter:blur(8px); -webkit-backdrop-filter:blur(8px);
  border:1px solid rgba(226,183,85,0.45); box-shadow:0 12px 30px rgba(0,0,0,0.5); animation:rise 0.25s ease-out;
}
.fc-name{font:700 1.1rem Cinzel, Georgia, serif; color:var(--accent-2);}
.fc-desc{font-size:0.8rem; color:var(--muted); margin:4px 0 10px; line-height:1.45;}
.fc-actions{display:flex; gap:6px;}
.fc-actions button{flex:1; padding:7px; border-radius:9px; border:1px solid var(--border); background:var(--panel-2); font-size:0.8rem;}
.fc-actions button:hover{border-color:var(--accent);}
.gallery-box{
  width:min(900px, 96vw); height:min(620px, 92dvh); display:flex; flex-direction:column;
  background:linear-gradient(180deg, #2a1f16, #120c08); border:1px solid rgba(226,183,85,0.5); border-radius:18px;
  box-shadow:0 24px 70px rgba(0,0,0,0.7); overflow:hidden; animation:rise 0.25s ease-out;
}
.gal-head{display:flex; align-items:center; justify-content:space-between; padding:10px 16px; border-bottom:1px solid var(--border);}
.gal-head h3{margin:0; font:700 1.15rem Cinzel, Georgia, serif; letter-spacing:0.05em;}
.gal-close{border:none; background:transparent; font-size:1.6rem; color:var(--muted); line-height:1;}
.gal-body{flex:1; min-height:0; display:flex;}
.gal-stage{position:relative; flex:1 1 60%; min-width:0;}
#galCanvas{position:absolute; inset:0; width:100%; height:100%; display:block; touch-action:none; cursor:grab;}
.gal-nav{position:absolute; top:50%; transform:translateY(-50%); width:40px; height:56px; border-radius:12px; border:1px solid var(--border); background:rgba(14,10,7,0.7); font-size:1.8rem; color:var(--accent-2);}
.gal-nav.prev{left:10px;} .gal-nav.next{right:10px;}
.gal-tip{position:absolute; left:50%; bottom:10px; transform:translateX(-50%); font-size:0.72rem; color:var(--muted); background:rgba(0,0,0,0.4); padding:3px 10px; border-radius:999px; white-space:nowrap;}
.gal-info{flex:0 0 300px; padding:14px 16px; overflow-y:auto; border-left:1px solid var(--border);}
.gal-name{font:700 1.4rem Cinzel, Georgia, serif; color:var(--accent-2);}
.gal-role{font-size:0.78rem; color:var(--accent); text-transform:uppercase; letter-spacing:0.08em; margin-top:2px;}
.gal-desc{font-size:0.85rem; color:var(--text); line-height:1.5; margin:10px 0 14px;}
.gal-list{display:grid; grid-template-columns:1fr 1fr; gap:6px;}
.gal-list .gl-side{grid-column:1 / -1; font-size:0.72rem; text-transform:uppercase; letter-spacing:0.08em; color:var(--muted); margin-top:6px;}
.gal-list button{padding:6px 8px; border-radius:9px; border:1px solid var(--border); background:var(--panel-2); font-size:0.78rem; text-align:left;}
.gal-list button.active{border-color:var(--accent); color:var(--accent-2); box-shadow:0 0 0 1px var(--accent) inset;}
@media (max-width: 700px), (orientation: portrait){
  .gal-body{flex-direction:column;}
  .gal-stage{flex:1 1 55%; min-height:240px;}
  .gal-info{flex:0 1 auto; border-left:none; border-top:1px solid var(--border); max-height:45%;}
}
