:root {
  color-scheme: dark;
  --ink: #28190f;
  --paper: #f7d483;
  --paper-dark: #b66a2c;
  --red: #b33a2e;
  --green: #4f8d42;
  --blue: #3c7ea2;
  --line: rgba(45, 28, 13, 0.42);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  width: 100%;
}

@supports (height: 100dvh) {
  html,
  body {
    height: 100dvh;
  }
}

body {
  background: #241f26;
  color: #fff3d3;
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  overflow: hidden;
  position: fixed;
  inset: 0;
}

button {
  border: 2px solid #6f3a1a;
  border-radius: 8px;
  background: linear-gradient(#f7cf74, #c8792c);
  color: #2b170c;
  cursor: pointer;
  font: 700 15px/1 "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  min-height: 38px;
  padding: 0 14px;
  box-shadow: inset 0 2px rgba(255, 255, 255, 0.45), 0 3px 0 #542813;
}

button:active {
  transform: translateY(2px);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.35), 0 1px 0 #542813;
}

button:disabled {
  filter: grayscale(0.8) brightness(0.7);
  cursor: not-allowed;
}

.shell {
  display: grid;
  height: 100%;
  place-items: center;
  padding: 14px;
}

@supports (height: 100dvh) {
  .shell {
    height: 100dvh;
  }
}

.game-wrap {
  aspect-ratio: 16 / 9;
  max-height: calc(100vh - 28px);
  max-width: calc(100vw - 28px);
  position: relative;
  width: min(100%, 1280px);
  background: #14100d;
  border: 3px solid #5e341c;
  border-radius: 8px;
  box-shadow: 0 18px 45px rgba(0, 0, 0, 0.45);
  overflow: hidden;
  touch-action: none;
}

canvas {
  display: block;
  height: 100%;
  image-rendering: auto;
  touch-action: none;
  width: 100%;
}

.hud {
  left: 14px;
  right: 14px;
  top: 12px;
  display: flex;
  gap: 10px;
  justify-content: space-between;
  pointer-events: none;
  position: absolute;
}

.stats,
.toolbar,
.panel,
.message {
  background: rgba(65, 36, 18, 0.86);
  border: 2px solid rgba(255, 215, 128, 0.75);
  box-shadow: 0 5px 18px rgba(0, 0, 0, 0.22);
}

.stats {
  align-items: center;
  border-radius: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  min-height: 48px;
  padding: 8px 12px;
  text-shadow: 0 2px #3c1c0c;
}

.stats span {
  white-space: nowrap;
}

.toolbar {
  align-items: center;
  border-radius: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 6px;
  pointer-events: auto;
}

.tool.selected {
  background: linear-gradient(#9ce06f, #4f9a3c);
}

.panel {
  border-radius: 8px;
  bottom: 14px;
  color: #fff4d8;
  left: 14px;
  max-width: min(360px, calc(100% - 28px));
  padding: 12px;
  position: absolute;
}

.panel p {
  margin: 6px 0 10px;
}

.panel-actions {
  display: flex;
  gap: 10px;
}

.hidden {
  display: none;
}

.message {
  border-radius: 8px;
  color: #fff4d8;
  left: 50%;
  min-width: min(460px, calc(100% - 28px));
  padding: 24px 26px;
  position: absolute;
  text-align: center;
  top: 50%;
  transform: translate(-50%, -50%);
}

.message h1 {
  font-size: 42px;
  margin: 0 0 6px;
  text-shadow: 0 3px #42200f;
}

.message-subtitle {
  color: #ffd98b;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0;
  margin: 0 0 12px;
  text-shadow: 0 2px #42200f;
}

.message p {
  margin: 0 0 18px;
}

.difficulty {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 0 0 16px;
}

.difficulty-btn {
  min-width: 72px;
}

.difficulty-btn.selected {
  background: linear-gradient(#9ce06f, #4f9a3c);
}

@media (max-width: 760px), (max-height: 520px), (pointer: coarse) and (max-width: 960px) {
  .shell {
    height: 100vh;
    padding: 0;
    width: 100vw;
  }

  @supports (height: 100dvh) {
    .shell {
      height: 100dvh;
      width: 100dvw;
    }
  }

  .game-wrap {
    border-radius: 0;
    border-width: 0;
    max-height: 100vh;
    max-width: 100vw;
    width: min(100vw, calc(100vh * 16 / 9));
  }

  @supports (height: 100dvh) {
    .game-wrap {
      max-height: 100dvh;
      max-width: 100dvw;
      width: min(100dvw, calc(100dvh * 16 / 9));
    }
  }

  .hud {
    align-items: flex-start;
    flex-direction: column;
    left: 8px;
    right: 8px;
    top: 8px;
  }

  .toolbar {
    flex-wrap: nowrap;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    white-space: nowrap;
  }

  .toolbar::-webkit-scrollbar {
    display: none;
  }

  button {
    font-size: 13px;
    min-height: 34px;
    padding: 0 10px;
    white-space: nowrap;
  }
}

@media (max-height: 520px) {
  .hud {
    gap: 5px;
    top: max(18px, env(safe-area-inset-top));
  }

  .stats {
    font-size: 11px;
    gap: 5px;
    min-height: 34px;
    padding: 5px 7px;
  }

  .toolbar {
    gap: 4px;
    padding: 4px;
  }

  button {
    font-size: 11px;
    min-height: 30px;
    padding: 0 7px;
  }

  .toolbar button {
    flex: 0 0 auto;
  }

  .message {
    min-width: min(460px, calc(100% - 24px));
    padding: 16px 20px;
    top: 61%;
  }

  .message h1 {
    font-size: 34px;
    margin-bottom: 4px;
  }

  .message-subtitle {
    font-size: 13px;
    margin-bottom: 8px;
  }

  .message p {
    margin-bottom: 12px;
  }

  .difficulty {
    margin-bottom: 12px;
  }
}

@media (max-width: 760px) and (orientation: portrait), (pointer: coarse) and (max-width: 960px) and (orientation: portrait) {
  .shell {
    height: 100vw;
    left: 50%;
    padding: 0;
    position: fixed;
    top: 50%;
    transform: translate(-50%, -50%) rotate(90deg);
    width: 100vh;
  }

  @supports (height: 100dvh) {
    .shell {
      height: 100dvw;
      width: 100dvh;
    }
  }

  .game-wrap {
    aspect-ratio: 16 / 9;
    border-radius: 0;
    border-width: 0;
    max-height: 100vw;
    max-width: 100vh;
    width: min(100vh, calc(100vw * 16 / 9));
  }

  @supports (height: 100dvh) {
    .game-wrap {
      max-height: 100dvw;
      max-width: 100dvh;
      width: min(100dvh, calc(100dvw * 16 / 9));
    }
  }

  .hud {
    align-items: flex-start;
    flex-direction: row;
    gap: 8px;
    left: 8px;
    right: 8px;
    top: max(14px, env(safe-area-inset-top));
  }

  .stats {
    font-size: 12px;
    gap: 6px;
    min-height: 40px;
    padding: 6px 8px;
  }

  .toolbar {
    gap: 6px;
    justify-content: flex-end;
  }

  .message {
    min-width: min(430px, calc(100% - 24px));
    padding: 18px 20px;
  }

  .message h1 {
    font-size: 34px;
  }

  .message-subtitle {
    font-size: 14px;
    margin-bottom: 10px;
  }
}
