* {
  box-sizing: border-box;
}

:root {
  --bg: #f6f0e7;
  --paper: #fffaf2;
  --ink: #2b2119;
  --muted: #74675c;
  --wood-dark: #5a321e;
  --wood: #7b4a2c;
  --wood-light: #a66a3f;
  --beam: #d7ad68;
  --rod: #d8c6a8;
  --bead: #241c17;
  --bead-hi: #4b3a30;
  --accent: #9b4b2d;
  --border: rgba(77, 47, 30, .16);
  --shadow: 0 16px 40px rgba(80, 54, 36, .12);
}

html {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 10% 0%, rgba(255, 255, 255, .8), transparent 32rem),
    linear-gradient(180deg, #fbf7f0 0%, var(--bg) 100%);
}

.adsbygoogle {
  margin: 16px 0;
}

footer {
  margin: 16px 0;
  text-align: center;
}

button {
  font: inherit;
}

.page {
  width: min(1500px, 100%);
  margin: 0 auto;
  padding: 34px 22px 70px;
}

.hero {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: end;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--accent);
  letter-spacing: .18em;
  font-size: 12px;
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: clamp(28px, 5vw, 48px);
  line-height: 1.1;
  letter-spacing: -.03em;
}

.lead {
  margin: 11px 0 0;
  color: var(--muted);
  font-size: 15px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.digit-switch {
  display: flex;
  padding: 4px;
  background: rgba(90, 50, 30, .08);
  border: 1px solid var(--border);
  border-radius: 13px;
}

.digit-btn,
.reset-btn {
  min-height: 42px;
  border: 0;
  border-radius: 9px;
  cursor: pointer;
  font-weight: 800;
  transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}

.digit-btn {
  min-width: 74px;
  padding: 0 16px;
  color: #6b594b;
  background: transparent;
}

.digit-btn.active {
  color: #fff;
  background: var(--wood);
  box-shadow: 0 5px 14px rgba(86, 51, 29, .2);
}

.reset-btn {
  padding: 0 18px;
  color: #fff;
  background: #34271f;
}

.digit-btn:active,
.reset-btn:active {
  transform: translateY(1px);
}

.value-card {
  display: flex;
  align-items: center;
  gap: 13px;
  margin: 18px 0;
  padding: 15px 18px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: rgba(255, 255, 255, .7);
  box-shadow: 0 8px 24px rgba(80, 54, 36, .07);
  overflow: hidden;
}

.value-label {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

#currentValue {
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: thin;
  white-space: nowrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: clamp(20px, 3vw, 30px);
  font-weight: 800;
  letter-spacing: .04em;
}


.value-input-card {
  margin: -2px 0 18px;
  padding: 15px 18px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: rgba(255, 255, 255, .7);
  box-shadow: 0 8px 24px rgba(80, 54, 36, .07);
}

.value-input-card>label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.value-input-row {
  display: flex;
  gap: 10px;
}

#valueInput {
  min-width: 0;
  flex: 1 1 auto;
  height: 46px;
  padding: 0 14px;
  border: 1px solid rgba(77, 47, 30, .24);
  border-radius: 10px;
  outline: none;
  background: #fff;
  color: var(--ink);
  font: 700 18px/1 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  letter-spacing: .03em;
}

#valueInput:focus {
  border-color: var(--wood);
  box-shadow: 0 0 0 3px rgba(123, 74, 44, .12);
}

.apply-value-btn {
  flex: 0 0 auto;
  min-height: 46px;
  padding: 0 20px;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  color: #fff;
  background: var(--wood);
  font-weight: 800;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.apply-value-btn:active {
  transform: translateY(1px);
}

.value-input-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 16px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.value-input-error {
  color: #b13b2b;
  font-weight: 700;
}

.soroban-wrap {
  position: relative;
}

.scroll-hint {
  display: none;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.soroban-scroller {
  overflow-x: auto;
  padding: 5px 0 18px;
  -webkit-overflow-scrolling: touch;
  scrollbar-color: #b69a7e transparent;
}

.soroban {
  --digits: 23;
  --col: 42px;
  --bead-h: 24px;
  --bead-w: 40px;
  position: relative;
  width: calc(var(--digits) * var(--col) + 48px);
  min-width: calc(var(--digits) * var(--col) + 48px);
  height: 228px;
  margin: 0 auto;
  padding: 12px 14px;
  border: 10px solid var(--wood-dark);
  border-radius: 15px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, .08), transparent 20%, rgba(0, 0, 0, .04) 60%, transparent),
    linear-gradient(#8d5634, #6f4026);
  box-shadow:
    inset 0 0 0 3px rgba(255, 255, 255, .08),
    inset 0 0 16px rgba(0, 0, 0, .3),
    var(--shadow);
  user-select: none;
  touch-action: pan-x;
}

.soroban-inner {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--digits), var(--col));
  height: 100%;
  border-radius: 4px;
  background: #f4e1bd;
  overflow: hidden;
  box-shadow: inset 0 0 16px rgba(60, 37, 22, .18);
}

.soroban-inner::after {
  content: "";
  position: absolute;
  z-index: 5;
  left: 0;
  right: 0;
  top: 48px;
  height: 16px;
  background:
    linear-gradient(180deg, #e8c783, #c6984d 50%, #e5bd72 52%, #bd8b43);
  border-top: 2px solid #9f7338;
  border-bottom: 2px solid #885f2f;
  box-shadow: 0 2px 5px rgba(60, 38, 20, .25);
  pointer-events: none;
}

.digit-column {
  position: relative;
  height: 100%;
  border-right: 1px solid rgba(93, 61, 34, .1);
}

.digit-column:last-child {
  border-right: 0;
}

.rod {
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 5px;
  transform: translateX(-50%);
  border-left: 1px solid rgba(84, 61, 38, .14);
  border-right: 1px solid rgba(255, 255, 255, .4);
  background: linear-gradient(90deg, #b9a078, #eee0c6 45%, #bca27a);
}

.heaven-zone,
.earth-zone {
  position: absolute;
  z-index: 2;
  left: 0;
  width: 100%;
}

.heaven-zone {
  top: 0;
  height: 48px;
}

.earth-zone {
  top: 64px;
  height: 120px;
}

.bead {
  position: absolute;
  z-index: 4;
  left: 50%;
  width: var(--bead-w);
  height: var(--bead-h);
  transform: translateX(-50%);
  border: 0;
  border-radius: 48% 48% 44% 44% / 54% 54% 46% 46%;
  background:
    radial-gradient(ellipse at 35% 22%, #615044 0 8%, transparent 9%),
    linear-gradient(180deg, #4e3b30 0%, #201915 45%, #110d0b 100%);
  box-shadow:
    inset 0 2px 2px rgba(255, 255, 255, .15),
    inset 0 -4px 5px rgba(0, 0, 0, .4),
    0 2px 3px rgba(62, 42, 29, .25);
  cursor: pointer;
  transition: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.bead:hover {
  filter: brightness(1.12);
}

.bead:active {
  transform: translateX(-50%) scale(.96);
}

.heaven-bead {
  top: 0;
}

.heaven-bead.active {
  top: 24px;
}

.earth-bead[data-index="0"] {
  top: 24px;
}

.earth-bead[data-index="1"] {
  top: 48px;
}

.earth-bead[data-index="2"] {
  top: 72px;
}

.earth-bead[data-index="3"] {
  top: 96px;
}

/* 梁に寄せた一珠。earth-zone の上端を基準に配置する */
.digit-column[data-earth="1"] .earth-bead[data-index="0"],
.digit-column[data-earth="2"] .earth-bead[data-index="0"],
.digit-column[data-earth="3"] .earth-bead[data-index="0"],
.digit-column[data-earth="4"] .earth-bead[data-index="0"] {
  top: 0;
}

.digit-column[data-earth="2"] .earth-bead[data-index="1"],
.digit-column[data-earth="3"] .earth-bead[data-index="1"],
.digit-column[data-earth="4"] .earth-bead[data-index="1"] {
  top: 24px;
}

.digit-column[data-earth="3"] .earth-bead[data-index="2"],
.digit-column[data-earth="4"] .earth-bead[data-index="2"] {
  top: 48px;
}

.digit-column[data-earth="4"] .earth-bead[data-index="3"] {
  top: 72px;
}

.place-mark {
  position: absolute;
  z-index: 7;
  top: 56px;
  left: 50%;
  width: 6px;
  height: 6px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: #6a351d;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .28);
}

.guide {
  margin-top: 34px;
  padding: clamp(24px, 5vw, 42px);
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.guide h2 {
  margin: 0 0 24px;
  font-size: clamp(23px, 3vw, 32px);
}

.guide-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.guide article {
  padding: 19px 20px;
  border-radius: 15px;
  background: #fff;
  border: 1px solid rgba(100, 66, 39, .11);
}

.guide h3 {
  margin: 0 0 9px;
  font-size: 17px;
}

.guide p {
  margin: 7px 0;
  color: #5f5349;
  line-height: 1.85;
  font-size: 14px;
}

.mini-reference {
  margin-top: 16px;
  padding: 15px 18px;
  border-radius: 13px;
  color: #5c402c;
  background: #f2e1c4;
  line-height: 1.7;
}

@media (max-width: 900px) {
  .hero {
    align-items: flex-start;
    flex-direction: column;
  }

  .controls {
    justify-content: flex-start;
  }

  .soroban {
    margin: 0;
  }

  .scroll-hint {
    display: block;
  }
}

@media (max-width: 620px) {
  .page {
    padding: 22px 14px 52px;
  }

  .controls {
    width: 100%;
    flex-wrap: nowrap;
    align-items: stretch;
  }

  .digit-switch {
    flex: 1 1 auto;
    min-width: 0;
  }

  .digit-btn {
    flex: 1;
    min-width: 0;
    padding-left: 10px;
    padding-right: 10px;
  }

  .reset-btn {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .value-card {
    align-items: flex-start;
    flex-direction: column;
    gap: 5px;
  }

  .value-input-row {
    flex-direction: column;
  }

  .apply-value-btn {
    width: 100%;
  }

  .guide-grid {
    grid-template-columns: 1fr;
  }
}

.bead {
  user-select: none;
  -webkit-user-select: none;
}