/* お絵描きトントン相撲：和風・紙工作の見た目 */

:root {
  --kinari: #f3e6c8;      /* 生成り（背景） */
  --kinari-deep: #e8d6aa;
  --paper: #fffaf0;       /* 紙 */
  --shu: #d9402a;         /* 朱 */
  --shu-deep: #a82a18;
  --sumi: #2a2420;        /* 墨 */
  --sumi-soft: #6b5d50;
  --dohyo: #c9a46a;
  --win: #d9402a;
  --lose: #3b5b86;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: "Hiragino Maru Gothic ProN", "Yu Gothic UI", "Hiragino Sans", "Meiryo", system-ui, sans-serif;
  --font-bold: "Hiragino Mincho ProN", "Yu Mincho", "Yu Gothic UI", "Hiragino Sans", "Meiryo", system-ui, sans-serif;
}

/* hidden 属性が display 指定に負けないように */
[hidden] { display: none !important; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--kinari);
  color: var(--sumi);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* ダブルタップズーム抑止 */
  overscroll-behavior: none;
}

body {
  /* 青海波っぽいうっすら柄 */
  background-image:
    radial-gradient(circle at 50% 100%, transparent 0 14px, rgba(176,140,80,.13) 15px 16px, transparent 17px 24px, rgba(176,140,80,.10) 25px 26px, transparent 27px),
    linear-gradient(180deg, #f7ecd0, #efdfb9);
  background-size: 56px 28px, 100% 100%;
}

button, input { font-family: inherit; }
button { -webkit-appearance: none; appearance: none; cursor: pointer; }
input { -webkit-user-select: text; user-select: text; }

#screen-title {
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(var(--safe-t) + 24px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 24px) calc(var(--safe-l) + 16px);
}
.home-card { max-width: 560px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.home-card .title { font-size: clamp(24px, 6vw, 36px); }
.home-card .btn { width: 100%; }
.home-art { width: 100%; height: auto; border: 3px solid var(--sumi); border-radius: 16px; box-shadow: 0 5px 0 var(--sumi); }
.howto { background: var(--paper); border: 2px solid var(--kinari-deep); border-radius: 16px; padding: 16px 20px; font-size: 14px; line-height: 1.7; }
.howto h2 { margin: 0; font-size: 18px; color: var(--shu); }
.howto ol { padding-left: 22px; margin: 8px 0; }
.howto p { margin: 8px 0 0; }
.home-links { display: flex; justify-content: center; flex-wrap: wrap; gap: 12px 24px; font-size: 13px; }
.home-links a { color: var(--sumi-soft); }
.home-back { align-self: center; background: none; color: var(--sumi-soft); border: 0; text-decoration: underline; padding: 8px 12px; font-size: 13px; }
.mode-buttons { display: grid; gap: 10px; width: 100%; }
.mode-label, .room-field { font-weight: 800; font-size: 14px; }
.friend-options { display: grid; gap: 9px; width: 100%; }
.friend-options input {
  border: 2px solid var(--sumi-soft); border-radius: 10px; background: var(--paper);
  padding: 12px; font: 800 22px system-ui; text-transform: uppercase; text-align: center; letter-spacing: .15em; width: 100%;
}
#screen-lobby { display: flex; align-items: center; justify-content: center; padding: 20px; min-height: 100dvh; }
.lobby-card { width: min(100%, 480px); padding: 26px; background: var(--paper); border: 2px solid var(--sumi); border-radius: 20px; text-align: center; }
.lobby-card h2 { font-size: 22px; }
.lobby-card p { margin: 18px 0; }
.room-code { display: block; font: 900 36px system-ui; letter-spacing: .12em; margin: 18px 0; user-select: text; }
.battle-back { position: absolute; top: 54px; right: 14px; padding: 7px 12px; border: 1px solid var(--sumi); border-radius: 8px; background: var(--paper); pointer-events: auto; font: inherit; }
button:focus-visible, a:focus-visible { outline: 3px solid var(--shu); outline-offset: 4px; }

/* ---------- お絵描き画面 ---------- */

#screen-draw {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 10px) calc(var(--safe-l) + 16px);
}

.draw-card {
  /* キャンバス一辺：幅・高さのどちらにも収まるように */
  --canvas: clamp(190px, min(100vw - 32px, 100dvh - 430px), 480px);
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.title-block { text-align: center; }

.title {
  margin: 0;
  font-family: var(--font-bold);
  font-weight: 900;
  font-size: 26px;
  line-height: 1.1;
  letter-spacing: .04em;
  color: var(--sumi);
  transform: rotate(-1.5deg);
  text-shadow: 2px 2px 0 var(--paper), 3px 3px 0 rgba(42,36,32,.25);
}
.title-accent { color: var(--shu); margin-left: .15em; }

.lead {
  margin: 4px 0 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--sumi-soft);
}

/* 紙のキャンバス */
.canvas-wrap {
  position: relative;
  width: var(--canvas);
  height: var(--canvas);
  background:
    radial-gradient(circle at 20% 15%, rgba(255,255,255,.7), transparent 55%),
    var(--paper);
  border: 3px solid var(--sumi);
  border-radius: 10px;
  box-shadow: 0 5px 0 var(--sumi), 0 10px 18px rgba(80,50,10,.25);
  transform: rotate(-.4deg);
  overflow: hidden;
}

#draw-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.guide {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  color: #b9a583;
  opacity: .55;
  pointer-events: none;
  transition: opacity .3s;
}
.canvas-wrap.has-ink .guide { opacity: .18; }

.draw-side {
  width: var(--canvas);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* 道具 */
.tools { display: flex; flex-direction: column; gap: 8px; }

.palette {
  display: flex;
  justify-content: space-between;
  gap: 4px;
}

.swatch {
  flex: 1 1 0;
  max-width: 44px;
  aspect-ratio: 1;
  min-width: 0;
  padding: 0;
  border-radius: 50%;
  background: var(--c);
  border: 3px solid var(--paper);
  box-shadow: 0 0 0 2px var(--sumi), 0 3px 0 2px var(--sumi);
  transition: transform .1s;
}
.swatch.is-active {
  transform: translateY(-3px) scale(1.1);
  box-shadow: 0 0 0 3px var(--shu), 0 4px 0 3px var(--shu-deep);
}

.tool-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
}

.sizes {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: var(--paper);
  border: 2px solid var(--sumi);
  border-radius: 12px;
}
.size-btn {
  width: 36px;
  min-height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  background: transparent;
  border: 0;
  border-radius: 9px;
}
.size-btn i {
  display: block;
  width: var(--d);
  height: var(--d);
  border-radius: 50%;
  background: var(--sumi);
}
.size-btn.is-active { background: #f9d9a8; box-shadow: inset 0 0 0 2px var(--shu); }

.tool-btn {
  flex: 1 1 0;
  min-width: 0;
  padding: 0 4px;
  min-height: 44px;
  font-size: 13px;
  font-weight: 800;
  color: var(--sumi);
  background: var(--paper);
  border: 2px solid var(--sumi);
  border-radius: 12px;
  box-shadow: 0 3px 0 var(--sumi);
  white-space: nowrap;
}
.tool-btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--sumi); }
.tool-btn.is-active { background: #f9d9a8; border-color: var(--shu); box-shadow: 0 3px 0 var(--shu-deep); color: var(--shu-deep); }
.tool-btn:disabled { opacity: .45; cursor: default; }
.tool-danger { color: var(--shu-deep); }

/* しこ名 */
.name-field {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 4px 12px;
  background: var(--paper);
  border: 2px solid var(--sumi);
  border-radius: 12px;
}
.name-label {
  flex: none;
  font-family: var(--font-bold);
  font-weight: 900;
  font-size: 14px;
  color: var(--shu);
  letter-spacing: .1em;
}
#name-input {
  flex: 1;
  min-width: 0;
  height: 36px;
  font-size: 18px; /* 16px 以上にして iOS の自動ズームを避ける */
  font-weight: 800;
  color: var(--sumi);
  background: transparent;
  border: 0;
  outline: 0;
}
#name-input::placeholder { color: #b8a98c; font-weight: 700; }
.name-field:focus-within { border-color: var(--shu); box-shadow: 0 0 0 3px rgba(217,64,42,.25); }

.draw-message {
  min-height: 1.3em;
  margin: 0;
  text-align: center;
  font-size: 13px;
  font-weight: 800;
  color: var(--shu-deep);
}

/* ボタン */
.btn {
  min-height: 52px;
  padding: 0 22px;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: .08em;
  color: var(--sumi);
  background: var(--paper);
  border: 3px solid var(--sumi);
  border-radius: 16px;
  box-shadow: 0 5px 0 var(--sumi);
  transition: transform .08s, box-shadow .08s;
}
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 1px 0 var(--sumi); }
.btn-primary { color: #fff; background: var(--shu); text-shadow: 0 2px 0 var(--shu-deep); box-shadow: 0 5px 0 var(--shu-deep), 0 5px 0 3px var(--sumi); }
.btn-primary:active:not(:disabled) { box-shadow: 0 1px 0 var(--shu-deep), 0 1px 0 3px var(--sumi); }
.btn-sub { background: #f9d9a8; }
.btn-ghost { background: transparent; box-shadow: none; border-style: dashed; font-size: 15px; min-height: 44px; }
.btn-ghost:active:not(:disabled) { box-shadow: none; }
.btn-big { min-height: 56px; font-family: var(--font-bold); font-size: 24px; letter-spacing: .16em; }
.btn:disabled {
  color: #9b8f80;
  text-shadow: none;
  background: #ddd2bd;
  border-color: #9b8f80;
  box-shadow: 0 5px 0 #9b8f80;
  cursor: default;
}

/* 横長の画面（PC）：キャンバスと道具を左右に並べる */
@media (min-aspect-ratio: 1/1) and (min-width: 760px) {
  .draw-card {
    --canvas: min(100dvh - 160px, 520px);
    max-width: 960px;
    display: grid;
    grid-template-columns: auto minmax(300px, 380px);
    grid-template-areas: "head head" "canvas side";
    justify-content: center;
    align-items: center;
    column-gap: 36px;
    row-gap: 14px;
  }
  .title-block { grid-area: head; }
  .title { font-size: 36px; }
  .lead { font-size: 15px; }
  .canvas-wrap { grid-area: canvas; }
  .draw-side { grid-area: side; width: 100%; gap: 14px; }
  .tools { gap: 14px; }
  .swatch { max-width: 40px; }
  .tool-btn { font-size: 14px; }
}

/* ---------- 対戦画面 ---------- */

#screen-battle {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: #3a2a1c;
  pointer-events: none; /* HUD はポインタを素通し */
  z-index: 10;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  pointer-events: auto;
}

.hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 12px) 0 calc(var(--safe-l) + 12px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}

.hud-rank {
  padding: 3px 18px;
  font-family: var(--font-bold);
  font-size: 15px;
  font-weight: 900;
  letter-spacing: .15em;
  color: var(--paper);
  background: var(--shu);
  border: 2px solid var(--sumi);
  border-radius: 6px;
  box-shadow: 0 3px 0 var(--sumi);
}
.hud-rank:empty { display: none; }

.hud-names {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.name-tag {
  flex: 1 1 0;
  min-width: 0;
  max-width: 40%;
  padding: 5px 10px;
  font-family: var(--font-bold);
  font-size: 17px;
  font-weight: 900;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--sumi);
  background: var(--paper);
  border: 3px solid var(--sumi);
  border-radius: 8px;
  box-shadow: 0 3px 0 var(--sumi);
}
.name-tag:empty { visibility: hidden; }
.name-player { transform: rotate(-1.5deg); border-left: 8px solid var(--shu); }
.name-rival { transform: rotate(1.5deg); border-right: 8px solid var(--lose); }

.hud-time {
  flex: none;
  min-width: 48px;
  margin-top: 3px;
  padding: 2px 8px;
  text-align: center;
  font-size: 15px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--paper);
  background: rgba(42,36,32,.55);
  border-radius: 999px;
}
.hud-time:empty { visibility: hidden; }

.btn-mute {
  position: absolute;
  top: calc(var(--safe-t) + 52px);
  right: calc(var(--safe-r) + 12px);
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 20px;
  font-weight: 900;
  line-height: 1;
  color: var(--sumi);
  background: var(--paper);
  border: 3px solid var(--sumi);
  border-radius: 50%;
  box-shadow: 0 3px 0 var(--sumi);
  pointer-events: auto;
}
.btn-mute:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--sumi); }
/* 消音中：main.js が class "muted" を付ければ斜線が出る（付けなくても動く） */
.btn-mute.muted { color: #9b8f80; background: #ddd2bd; }
.btn-mute.muted::after {
  content: "";
  position: absolute;
  left: 8px; right: 8px; top: 50%;
  height: 3px;
  background: var(--shu);
  border-radius: 2px;
  transform: rotate(-45deg);
}

/* 中央の大きな文字 */
.banner {
  position: absolute;
  left: 0; right: 0;
  top: 38%;
  transform: translateY(-50%);
  text-align: center;
  font-family: var(--font-bold);
  font-size: clamp(44px, 14vmin, 96px);
  font-weight: 900;
  letter-spacing: .12em;
  color: var(--paper);
  -webkit-text-stroke: 3px var(--sumi);
  paint-order: stroke fill;
  text-shadow: 0 5px 0 var(--shu), 0 9px 0 var(--sumi), 0 14px 22px rgba(0,0,0,.35);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
}
.banner.show { animation: banner-pop 1.5s cubic-bezier(.2,.8,.3,1) both; }

@keyframes banner-pop {
  0%   { opacity: 0; transform: translateY(-50%) scale(.3) rotate(-6deg); }
  14%  { opacity: 1; transform: translateY(-50%) scale(1.25) rotate(2deg); }
  26%  { opacity: 1; transform: translateY(-50%) scale(1) rotate(0); }
  72%  { opacity: 1; transform: translateY(-50%) scale(1.03); }
  100% { opacity: 0; transform: translateY(-58%) scale(1.1); }
}

/* 「ここをトントン！」 */
.tap-hint {
  position: absolute;
  left: 0; right: 0;
  bottom: calc(var(--safe-b) + 7vh);
  display: flex;
  justify-content: center;
  pointer-events: none;
}
.tap-hint span {
  padding: 8px 22px;
  font-family: var(--font-bold);
  font-size: 20px;
  font-weight: 900;
  letter-spacing: .1em;
  color: var(--sumi);
  background: rgba(255,250,240,.92);
  border: 3px solid var(--sumi);
  border-radius: 999px;
  box-shadow: 0 4px 0 var(--sumi);
  animation: hint-bob .8s ease-in-out infinite alternate;
}
.tap-hint span::after { content: " 👇"; }
@keyframes hint-bob {
  from { transform: translateY(0); }
  to   { transform: translateY(-8px); }
}

/* ---------- 結果オーバーレイ ---------- */

#screen-result {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-t) + 16px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 16px);
  background: rgba(42,36,32,.55);
  animation: result-fade .35s ease-out both;
  --tone: var(--sumi);
  --tone-deep: #000;
}
#screen-result[data-outcome="win"]  { --tone: var(--win);  --tone-deep: var(--shu-deep); background: radial-gradient(circle at 50% 40%, rgba(255,214,120,.55), rgba(42,36,32,.6) 70%); }
#screen-result[data-outcome="lose"] { --tone: var(--lose); --tone-deep: #263b58; background: rgba(20,28,44,.62); }

@keyframes result-fade { from { opacity: 0; } to { opacity: 1; } }

.result-card {
  width: 100%;
  max-width: 400px;
  padding: 26px 20px 22px;
  text-align: center;
  background: var(--paper);
  border: 4px solid var(--sumi);
  border-radius: 20px;
  box-shadow: 0 8px 0 var(--sumi), 0 18px 30px rgba(0,0,0,.35);
  animation: card-pop .5s cubic-bezier(.2,1.3,.4,1) both;
}
@keyframes card-pop {
  from { opacity: 0; transform: translateY(24px) scale(.8) rotate(-3deg); }
  to   { opacity: 1; transform: none; }
}

.result-title {
  margin: 0;
  font-family: var(--font-bold);
  font-size: clamp(54px, 18vw, 84px);
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: .1em;
  color: var(--tone);
  text-shadow: 0 4px 0 var(--tone-deep), 0 6px 0 rgba(42,36,32,.25);
  transform: rotate(-2deg);
}
#screen-result[data-outcome="win"] .result-title { animation: win-bounce 1s ease-in-out infinite alternate; }
@keyframes win-bounce {
  from { transform: rotate(-2deg) scale(1); }
  to   { transform: rotate(2deg) scale(1.07); }
}

.result-kimarite {
  display: inline-block;
  margin: 10px 0 0;
  padding: 3px 16px;
  font-family: var(--font-bold);
  font-size: 18px;
  font-weight: 900;
  letter-spacing: .1em;
  color: var(--paper);
  background: var(--tone);
  border-radius: 6px;
}
.result-kimarite:empty { display: none; }

.result-message {
  min-height: 1.4em;
  margin: 12px 0 0;
  font-size: 16px;
  font-weight: 800;
  line-height: 1.5;
  color: var(--sumi-soft);
}

.result-buttons {
  margin-top: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.result-buttons .btn { width: 100%; }
.result-buttons .btn-primary { background: var(--tone); text-shadow: 0 2px 0 var(--tone-deep); box-shadow: 0 5px 0 var(--tone-deep), 0 5px 0 3px var(--sumi); }
#screen-result:not([data-outcome]) .btn-primary { background: var(--shu); }

/* 低い画面（横向きスマホなど）では詰める */
@media (max-height: 520px) {
  .result-card { padding: 14px 16px; }
  .result-title { font-size: 44px; }
  .result-buttons { flex-direction: row; flex-wrap: wrap; margin-top: 10px; }
  .result-buttons .btn { flex: 1 1 30%; width: auto; padding: 0 8px; font-size: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  .tap-hint span, #screen-result[data-outcome="win"] .result-title { animation: none; }
}
