/* ============================================================
   CIPHER TOOL — Light Theme
   ============================================================ */

:root {
  --bg-deep: #eef0f5;
  --bg: #ffffff;
  --surface: #f4f5f9;
  --surface-raised: #eceef4;
  --border: #d2d7e3;
  --border-bright: #bac0d0;
  --text: #2e3344;
  --text-dim: #838da5;
  --text-bright: #141820;
  --accent: #168dab;
  --accent-soft: rgba(22, 141, 171, 0.08);
  --green: #16a34a;
  --mono: "JetBrains Mono", "Menlo", monospace;
  --sans: "Chakra Petch", sans-serif;
  --radius: 8px;
  --radius-lg: 12px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* ---- Base ---- */
body {
  font-family: var(--sans);
  background: var(--bg-deep);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 48px 16px 64px;
}

.bg-grid {
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(rgba(22, 141, 171, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(22, 141, 171, 0.03) 1px, transparent 1px);
  background-size: 48px 48px;
  pointer-events: none;
}

.app {
  width: 100%;
  max-width: 600px;
  position: relative;
  z-index: 1;
}

/* ---- Header ---- */
.header {
  text-align: center;
  margin-bottom: 28px;
}

.title {
  font-size: 40px;
  font-weight: 700;
  letter-spacing: 0.35em;
  color: var(--accent);
}

.header-rule {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 12px auto 10px;
  max-width: 240px;
}

.header-rule::before,
.header-rule::after {
  content: "";
  flex: 1;
  height: 1px;
  background: linear-gradient(to var(--dir, right), transparent, var(--border));
}

.header-rule::after {
  --dir: left;
}

.header-diamond {
  width: 6px;
  height: 6px;
  background: var(--accent);
  transform: rotate(45deg);
  flex-shrink: 0;
}

.subtitle {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  letter-spacing: 0.2em;
}

/* ---- Tabs ---- */
.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 4px;
  border: 1px solid var(--border);
  border-bottom: none;
}

.tab {
  flex: 1;
  padding: 12px 8px;
  font-family: var(--sans);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  background: transparent;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: color 0.2s, background 0.2s;
}

.tab:hover {
  color: var(--text);
  background: rgba(0, 0, 0, 0.03);
}

.tab.active {
  color: var(--accent);
  background: var(--accent-soft);
}

/* ---- Panel ---- */
.panel {
  display: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  padding: 24px;
  margin-top: -1px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.panel.active {
  display: block;
}

/* ---- Mode chips ---- */
.mode-select {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.chip {
  cursor: pointer;
  user-select: none;
}

.chip input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

.chip span {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 16px 7px 10px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--text-dim);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 24px;
  transition: color 0.2s, border-color 0.2s, background 0.2s, opacity 0.2s;
}

/* ラジオ円 */
.chip span::before {
  content: "";
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--border-bright);
  background: var(--bg);
  flex-shrink: 0;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

.chip:hover span {
  color: var(--accent);
  border-color: var(--accent);
}

.chip:hover span::before {
  border-color: var(--accent);
}

.chip input:checked + span {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent);
}

.chip input:checked + span:hover {
  opacity: 0.5;
}

.chip input:checked + span::before {
  border-color: #fff;
  background: #fff;
  box-shadow: inset 0 0 0 2px var(--accent);
}

/* ---- Field group ---- */
.field-group {
  margin-bottom: 16px;
}

.field-group:last-child {
  margin-bottom: 0;
}

.field-label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.16em;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.field-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.field-header .field-label {
  margin-bottom: 0;
}

/* ---- Textarea ---- */
textarea {
  width: 100%;
  min-height: 108px;
  padding: 14px 16px;
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-bright);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  transition: border-color 0.2s, box-shadow 0.2s;
}

textarea::placeholder {
  color: var(--text-dim);
  font-family: var(--sans);
  font-size: 13px;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea::-webkit-scrollbar {
  width: 6px;
}

textarea::-webkit-scrollbar-track {
  background: transparent;
}

textarea::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* ---- Key input (Vigenère) ---- */
.key-input {
  width: 100%;
  padding: 10px 16px;
  font-family: var(--mono);
  font-size: 14px;
  color: var(--text-bright);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.2s, box-shadow 0.2s;
  letter-spacing: 0.08em;
}

.key-input::placeholder {
  color: var(--text-dim);
  font-family: var(--sans);
  font-size: 13px;
  letter-spacing: normal;
}

.key-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ---- Shift control ---- */
.shift-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.shift-control {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}

.shift-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-family: var(--sans);
  color: var(--text-dim);
  background: transparent;
  border: none;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}

.shift-btn:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.shift-btn:active {
  background: rgba(22, 141, 171, 0.14);
}

.shift-control input[type="number"] {
  width: 56px;
  padding: 6px 0;
  font-family: var(--mono);
  font-size: 16px;
  font-weight: 500;
  text-align: center;
  color: var(--accent);
  background: transparent;
  border: none;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  -moz-appearance: textfield;
}

.shift-control input[type="number"]::-webkit-inner-spin-button,
.shift-control input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.shift-control input[type="number"]:focus {
  outline: none;
  background: var(--accent-soft);
}

.shift-label {
  font-size: 13px;
  color: var(--text-dim);
  letter-spacing: 0.04em;
}

/* ---- Copy button ---- */
.copy-btn {
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 10px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, background 0.2s;
}

.copy-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.copy-btn.copied {
  color: var(--green);
  border-color: var(--green);
}

/* ---- Result ---- */
.result {
  padding: 14px 16px;
  min-height: 48px;
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.7;
  word-break: break-all;
  white-space: pre-wrap;
  color: var(--text-bright);
  background: var(--surface);
  border: none;
  border-left: 2px solid transparent;
  border-radius: var(--radius);
  transition: border-color 0.3s;
}

.result:not(:empty) {
  border-left: 2px solid var(--accent);
}

.result:empty::before {
  content: "結果がここに表示されます";
  color: var(--text-dim);
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: 0.04em;
}

/* ---- Section title ---- */
.section-title {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-bottom: 12px;
}

/* ---- Morse section ---- */
.section-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

.morse-buttons {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

.morse-btn {
  padding: 6px 18px;
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.morse-btn:last-child {
  font-size: 12px;
}

.morse-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.morse-btn:active {
  background: rgba(22, 141, 171, 0.14);
}

/* ---- Polybius grid ---- */
.polybius-grid {
  margin: 16px 0 20px;
  border-collapse: separate;
  border-spacing: 4px;
  font-family: var(--mono);
  font-size: 13px;
  user-select: none;
}

.polybius-grid th,
.polybius-grid td {
  width: 38px;
  height: 32px;
  text-align: center;
  border-radius: 4px;
}

.polybius-grid th {
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
  font-size: 11px;
}

.polybius-grid td {
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}

.polybius-grid tr:hover th,
.polybius-grid th.col-hover {
  background: var(--accent-soft);
}

.polybius-grid td:hover {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent);
}

/* ---- Info link ---- */
.info-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 500;
  color: var(--accent);
  background: none;
  border: none;
  padding: 0;
  margin-top: 14px;
  cursor: pointer;
  text-decoration: none;
}

.info-link::before {
  content: "?";
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  border: 1.5px solid var(--accent);
  border-radius: 50%;
  flex-shrink: 0;
}

.info-link span {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.info-link:hover {
  opacity: 0.6;
}

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--bg);
  border-radius: var(--radius-lg);
  max-width: 520px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 0;
}

.modal-title {
  font-family: var(--sans);
  font-size: 16px;
  font-weight: 700;
  color: var(--text-bright);
  letter-spacing: 0.02em;
}

.modal-close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: var(--text-dim);
  background: none;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.modal-close:hover {
  color: var(--text-bright);
  background: var(--surface);
}

.modal-body {
  padding: 16px 24px 24px;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.8;
  color: var(--text);
}

.modal-body h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.04em;
  margin: 16px 0 6px;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body p {
  margin: 0 0 10px;
}

.modal-body a {
  color: var(--accent);
  text-decoration: none;
}

.modal-body a:hover {
  text-decoration: underline;
}

.modal-body code {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--surface);
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--accent);
}

/* ---- Site footer ---- */
.site-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
}

.site-footer__link {
  font-family: var(--sans);
  font-size: 10px;
  color: var(--text-dim);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
}

.site-footer__link:hover {
  text-decoration: underline;
}

.site-footer__sep {
  font-size: 10px;
  color: var(--text-dim);
}

/* ---- Site credit ---- */
.site-credit {
  position: fixed;
  bottom: 12px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 10px;
  color: var(--text-dim);
  letter-spacing: 0.02em;
  pointer-events: none;
}

.site-credit a {
  color: var(--text-dim);
  text-decoration: none;
  pointer-events: auto;
}

.site-credit a:hover {
  text-decoration: underline;
}

/* ---- Responsive ---- */
@media (max-width: 480px) {
  body {
    padding: 24px 10px 48px;
  }

  .title {
    font-size: 30px;
    letter-spacing: 0.25em;
  }

  .tabs {
    border-radius: var(--radius) var(--radius) 0 0;
  }

  .tab {
    font-size: 12px;
    padding: 10px 4px;
  }

  .panel {
    padding: 16px;
    border-radius: 0 0 var(--radius) var(--radius);
  }

  textarea {
    font-size: 13px;
    min-height: 90px;
  }

  .chip span {
    padding: 5px 12px;
    font-size: 12px;
  }
}
