* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-body, "Hiragino Kaku Gothic ProN", "Meiryo", sans-serif);
  background: var(--color-bg, #f5f5f2);
  color: var(--color-text, #2c2c2a);
  padding-bottom: 3rem;
  transition: background-color 0.3s ease, color 0.3s ease;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--color-header-bg, #3c3489);
  color: var(--color-header-text, #fff);
  padding: 1.25rem 0.75rem;
}

.app-header h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-family: var(--font-heading, inherit);
  font-size: clamp(1.05rem, 5vw, 1.5rem);
  letter-spacing: 0.02em;
  text-align: center;
  overflow-wrap: break-word;
}

.header-spacer,
.menu-button {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}

.menu-button {
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--color-header-text, #fff);
  cursor: pointer;
  padding: 0;
}

.section-menu {
  position: sticky;
  top: 0;
  z-index: 200;
  background: var(--color-surface, #fff);
  border-bottom: 2px solid var(--color-border, #eee);
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 640px;
  margin: 0 auto;
}

.section-menu[hidden] {
  display: none;
}

.section-menu-item {
  text-align: left;
  padding: 10px 14px;
  background: none;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: bold;
  color: var(--color-text, #333);
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.section-menu-item:hover {
  background: var(--color-primary-tint, #f0f0f0);
}

@media (min-width: 768px) {
  .header-spacer,
  .menu-button {
    display: none;
  }

  .section-menu {
    position: static;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: center;
    max-width: none;
    gap: 4px;
    padding: 0.6rem 1rem;
    background: var(--color-surface, #fff);
    border-bottom: 2px solid var(--color-border, #eee);
    overflow-x: auto;
  }

  .section-menu[hidden] {
    display: flex;
  }

  .section-menu-item {
    border-radius: 999px;
    border: 2px solid var(--color-border, #eee);
    padding: 7px 14px;
    font-size: 0.85rem;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background-color 0.15s ease, border-color 0.15s ease;
  }

  .section-menu-item:hover {
    border-color: var(--color-primary, #FF6B4A);
    background: var(--color-primary-tint, #FFE9E2);
  }
}

.back-to-top {
  display: block;
  margin: 1rem auto 0;
  padding: 6px 16px;
  background: none;
  border: 1px solid var(--color-border, #ccc);
  border-radius: 999px;
  color: var(--color-text-muted, #888);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.back-to-top:hover {
  background: var(--color-primary-tint, #f0f0f0);
}

main section[id] {
  scroll-margin-top: 12px;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem;
}

section {
  background: var(--color-surface, #fff);
  border-radius: var(--radius-card, 12px);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  box-shadow: var(--shadow-card, 0 1px 3px rgba(0,0,0,0.08));
  border: var(--border-card, none);
}

h2 {
  font-family: var(--font-heading, inherit);
  font-size: 1.2rem;
  margin-top: 0;
  color: var(--color-heading, var(--color-text, #2c2c2a));
}

.dashboard {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  background: none;
  box-shadow: none;
  padding: 0;
}

.dashboard-card {
  background: var(--color-surface, #fff);
  border-radius: var(--radius-card, 12px);
  padding: 1rem;
  text-align: center;
  box-shadow: var(--shadow-card, 0 1px 3px rgba(0,0,0,0.08));
  border: var(--border-card, none);
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

@media (hover: hover) {
  .dashboard-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 14px rgba(0,0,0,0.12);
  }
}

.dashboard-card.tap-active {
  transform: translateY(-3px);
  box-shadow: 0 6px 14px rgba(0,0,0,0.12);
}

.dashboard-card:active {
  transform: translateY(-1px) scale(0.97);
}

.dashboard-label {
  margin: 0 0 4px;
  font-size: 0.8rem;
  color: var(--color-text-muted, #666);
}

.dashboard-value {
  margin: 0;
  font-family: var(--font-heading, inherit);
  font-size: 1.6rem;
  font-weight: bold;
  color: var(--color-primary, #3c3489);
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 1rem;
}

.form-row label {
  font-size: 0.9rem;
  color: var(--color-text-muted, #555);
}

.form-row input {
  padding: 10px;
  border: 1px solid var(--color-border, #ccc);
  border-radius: var(--radius-button, 8px);
  font-size: 1rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.exam-name-selects {
  display: flex;
  gap: 8px;
}

.exam-name-selects select {
  flex: 1;
  padding: 10px 6px;
  border: 1px solid var(--color-border, #ccc);
  border-radius: var(--radius-button, 8px);
  font-size: 0.95rem;
  background: var(--color-surface, #fff);
  color: var(--color-text, #2c2c2a);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-row input:focus,
.exam-name-selects select:focus,
.result-table input[type="number"]:focus {
  outline: none;
  border-color: var(--color-primary, #3c3489);
  box-shadow: 0 0 0 3px var(--color-primary-tint, rgba(60,52,137,0.15));
}

.button-row {
  display: flex;
  gap: 8px;
  position: relative;
}

.button-row .primary-button,
.button-row .secondary-button {
  width: auto;
  flex: 1;
}

.primary-button {
  width: 100%;
  padding: 12px;
  background: var(--color-primary, #d85a30);
  color: var(--color-primary-text, #fff);
  border: var(--border-button, none);
  border-radius: var(--radius-button, 8px);
  font-size: 1.05rem;
  font-weight: bold;
  cursor: pointer;
}

.primary-button:hover {
  background: var(--color-primary-hover, #b9491f);
}

.error-message {
  color: var(--color-danger, #c62828);
  font-size: 0.9rem;
  min-height: 1.2em;
  margin: 8px 0 0;
}

.result-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}

.result-table th, .result-table td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--color-border, #eee);
  font-size: 0.9rem;
}

.result-table input[type="number"] {
  width: 64px;
  padding: 6px;
  border: 1px solid var(--color-border, #ccc);
  border-radius: 6px;
  font-size: 0.95rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.result-total {
  font-family: var(--font-heading, inherit);
  font-size: 1.25rem;
  font-weight: bold;
  text-align: right;
  color: var(--color-primary, #d85a30);
  margin: 0 0 12px;
}

.secondary-button {
  width: 100%;
  padding: 10px;
  background: var(--color-surface, #fff);
  color: var(--color-primary, #3c3489);
  border: 1px solid var(--color-primary, #3c3489);
  border-radius: var(--radius-button, 8px);
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.secondary-button:hover {
  background: var(--color-primary-tint, #f0eefc);
}

.update-message {
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-bottom: 10px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
}

.update-message.pop-in:empty {
  display: none;
}

.update-message.pop-in {
  display: inline-block;
  background: var(--color-primary, #2e7d32);
  color: var(--color-primary-text, #fff);
  font-weight: bold;
  font-size: 0.95rem;
  padding: 8px 20px;
  border-radius: 999px;
  box-shadow: 0 3px 0 var(--color-primary-hover, #1b5e20);
  animation: bubblePopInOut 2s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.update-message.pop-in::after {
  content: "";
  position: absolute;
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 7px solid var(--color-primary, #1b5e20);
}

@keyframes bubblePopInOut {
  0% { opacity: 0; transform: translateX(-50%) translateY(16px) scale(0.7); }
  15% { opacity: 1; transform: translateX(-50%) translateY(-3px) scale(1.08); }
  25% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  80% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-10px) scale(0.85); }
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-item {
  border: 1px solid var(--color-border, #eee);
  border-radius: var(--radius-button, 10px);
  padding: 10px 12px;
  background: var(--color-history-item-bg, transparent);
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.history-item-header:hover {
  opacity: 0.85;
}

.history-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}

.history-item-title {
  font-weight: bold;
}

.history-item-date {
  font-size: 0.8rem;
  color: var(--color-text-muted, #888);
}

.history-item-total {
  color: var(--color-primary, #d85a30);
  font-weight: bold;
}

.history-item-detail {
  margin-top: 0;
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.25s ease, margin-top 0.3s ease;
}

.history-item-detail.open {
  margin-top: 8px;
  max-height: 600px;
  opacity: 1;
}

.history-item-detail table {
  width: 100%;
  border-collapse: collapse;
}

.history-item-detail th, .history-item-detail td {
  text-align: left;
  padding: 4px 6px;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--color-border, #f0f0f0);
}

.score-arrow {
  display: inline-block;
  margin-left: 4px;
  font-weight: bold;
}

.score-arrow.up {
  color: #06b6d4;
}

.score-arrow.down {
  color: #e8536b;
}

.score-arrow.same {
  color: #8b5cf6;
}

.history-item-buttons {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.edit-button, .delete-button {
  background: none;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.edit-button:active, .delete-button:active {
  transform: scale(0.96);
}

.edit-button {
  border: 1px solid var(--color-primary, #3c3489);
  color: var(--color-primary, #3c3489);
}

.edit-button:hover {
  background: var(--color-primary-tint, #f0eefc);
}

.delete-button {
  border: 1px solid var(--color-danger, #c62828);
  color: var(--color-danger, #c62828);
}

.delete-button:hover {
  background: var(--color-danger-tint, #fdecea);
}

.empty-message {
  color: var(--color-text-muted, #999);
  font-size: 0.9rem;
}

.allclear-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.15);
  animation: allclearFadeIn 0.3s ease;
  cursor: pointer;
}

@keyframes allclearFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.allclear-card {
  background: var(--color-surface, #fff);
  border-radius: 28px;
  padding: 32px 40px;
  text-align: center;
  box-shadow: 0 10px 0 var(--color-primary-hover, #b9491f);
  animation: allclearPop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 85%;
  cursor: default;
}

@keyframes allclearPop {
  0% { transform: scale(0.5); opacity: 0; }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

.allclear-emoji {
  font-size: 3rem;
  display: block;
  margin-bottom: 8px;
}

.allclear-title {
  font-family: var(--font-heading, inherit);
  font-size: 1.6rem;
  font-weight: bold;
  color: var(--color-primary, #FF6B4A);
  margin: 0 0 4px;
}

.allclear-subtitle {
  font-size: 0.95rem;
  color: var(--color-text-muted, #888);
  margin: 0;
}

.confetti-piece {
  position: fixed;
  bottom: -40px;
  font-size: 1.6rem;
  z-index: 999;
  pointer-events: none;
  animation-name: confettiRise;
  animation-timing-function: ease-out;
  animation-fill-mode: forwards;
}

@keyframes confettiRise {
  0% { transform: translateY(0) translateX(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(-110vh) translateX(var(--drift, 0px)) rotate(360deg); opacity: 0; }
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ranking-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ranking-badge {
  position: relative;
  font-size: 0.95rem;
  border-radius: 999px;
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: default;
}

.ranking-badge.good {
  background: radial-gradient(circle, rgba(255,217,61,0.55) 0%, rgba(255,217,61,0) 70%);
}

.ranking-badge.bad {
  background: radial-gradient(circle, rgba(107,203,119,0.4) 0%, rgba(107,203,119,0) 70%);
}

.ranking-badge.none {
  visibility: hidden;
}

.ranking-badge-tooltip {
  position: absolute;
  bottom: 130%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-primary, #FF6B4A);
  color: var(--color-primary-text, #fff);
  font-size: 0.7rem;
  font-weight: bold;
  padding: 4px 10px;
  border-radius: 6px;
  white-space: nowrap;
  z-index: 20;
  pointer-events: none;
  animation: tooltipPopIn 0.15s ease;
}

.ranking-badge-tooltip::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 4px solid var(--color-primary, #FF6B4A);
}

@keyframes tooltipPopIn {
  from { opacity: 0; transform: translateX(-50%) translateY(4px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.ranking-subject {
  width: 60px;
  font-size: 0.85rem;
  font-weight: bold;
  flex-shrink: 0;
}

.ranking-bar-wrap {
  flex: 1;
  height: 10px;
  background: var(--color-border, #eee);
  border-radius: 999px;
  overflow: hidden;
}

.ranking-bar {
  height: 100%;
  background: var(--color-primary, #FF6B4A);
  border-radius: 999px;
  transition: width 0.5s ease;
}

.ranking-score {
  font-size: 0.85rem;
  font-weight: bold;
  width: 64px;
  text-align: right;
  flex-shrink: 0;
}

.goal-input-row {
  display: flex;
  gap: 8px;
}

.goal-input-row input {
  flex: 1;
  padding: 10px;
  border: 1px solid var(--color-border, #ccc);
  border-radius: var(--radius-button, 8px);
  font-size: 1rem;
}

.goal-input-row button {
  width: auto;
  padding: 10px 16px;
  flex-shrink: 0;
}

.goal-progress {
  margin-top: 1rem;
}

.goal-gauge-wrap {
  height: 22px;
  background: var(--color-border, #eee);
  border-radius: 999px;
  overflow: hidden;
}

.goal-gauge {
  height: 100%;
  background: var(--color-primary, #FF6B4A);
  border-radius: 999px;
  transition: width 0.6s cubic-bezier(0.34, 1.2, 0.64, 1);
}

.goal-gauge.achieved {
  background: linear-gradient(90deg, #FF6B6B, #FFB86B, #FFD93D, #6BCB77, #4D96FF, #9B6BFF, #FF6BB5, #FF6B6B);
  background-size: 400% 100%;
  animation: rainbowShift 18s linear infinite, gaugePulse 0.6s ease;
}

@keyframes rainbowShift {
  0% { background-position: 400% 50%; }
  100% { background-position: 0% 50%; }
}

@keyframes gaugePulse {
  0% { filter: brightness(1); }
  50% { filter: brightness(1.12); }
  100% { filter: brightness(1); }
}

.goal-status {
  margin: 10px 0 0;
  font-size: 0.9rem;
  font-weight: bold;
  text-align: center;
  color: var(--color-text-muted, #888);
}

.chart-toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 1rem;
}

.subject-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
}

.chart-reset-button {
  width: auto;
  padding: 5px 14px;
  font-size: 0.75rem;
  flex-shrink: 0;
}

.subject-toggle {
  padding: 5px 12px;
  border-radius: 999px;
  border: 2px solid var(--color-border, #ccc);
  background: var(--color-surface, #fff);
  color: var(--color-text-muted, #888);
  font-size: 0.75rem;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.chart-scroll-wrap {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  border-radius: 12px;
}

.chart-canvas-wrap {
  position: relative;
  height: 260px;
}

.tiers-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}

.tiers-table th {
  text-align: left;
  padding: 4px 6px;
  font-size: 0.8rem;
  color: var(--color-text-muted, #888);
}

.tiers-table td {
  padding: 6px;
  vertical-align: middle;
}

.tiers-table input[type="number"] {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--color-border, #ccc);
  border-radius: 6px;
  font-size: 0.95rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.tiers-table input[type="number"]:focus {
  outline: none;
  border-color: var(--color-primary, #3c3489);
  box-shadow: 0 0 0 3px var(--color-primary-tint, rgba(60,52,137,0.15));
}

.tiers-table .tier-unit {
  font-size: 0.85rem;
  color: var(--color-text-muted, #888);
  white-space: nowrap;
}

.tier-delete-button {
  background: none;
  border: 1px solid var(--color-danger, #c62828);
  color: var(--color-danger, #c62828);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.tier-delete-button:hover {
  background: var(--color-danger-tint, #fdecea);
}

.tiers-button-row {
  margin-bottom: 0.75rem;
}

.simple-message {
  margin: 8px 0 0;
  font-size: 0.9rem;
  font-weight: bold;
  text-align: center;
  color: var(--color-success, #2FAE7E);
  min-height: 1.2em;
}

.simple-message.pop-in {
  animation: simpleMessageFadeIn 0.3s ease;
}

@keyframes simpleMessageFadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
