:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1c1c1c;
  --text-muted: #6b6b6b;
  --border: #e0e0dd;
  --accent: #2f6f4f;
  --accent-contrast: #ffffff;
  --danger: #b3441e;
  --warn: #a17a00;
  --ok: #2f6f4f;
  --shadow: 0 2px 10px rgba(0,0,0,0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1f2226;
    --text: #ececec;
    --text-muted: #9a9a9a;
    --border: #33373d;
    --accent: #5fbf8f;
    --accent-contrast: #0d1a14;
    --danger: #e08a63;
    --warn: #e0c368;
    --ok: #5fbf8f;
    --shadow: 0 2px 10px rgba(0,0,0,0.4);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem 1rem 3rem;
  min-height: 100vh;
  position: relative;
}

.app-header {
  position: relative;
  text-align: center;
  padding: 1.5rem 0 1rem;
}

.app-header h1 {
  margin: 0;
  font-size: 1.6rem;
}

.app-title-icon {
  width: 1.15em;
  height: 1.15em;
  margin-right: 0.3em;
  border-radius: 22%;
  vertical-align: -0.2em;
}

.tagline {
  margin: 0.25rem 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--text);
  padding: 0.25rem 0.5rem;
}

#settingsBtn {
  position: absolute;
  top: 1.25rem;
  right: 0;
}

.search-section {
  margin-bottom: 1.5rem;
}

#searchForm {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.clear-btn {
  background: none;
  color: var(--text-muted);
  border-color: var(--border);
}

#searchInput {
  flex: 1;
  padding: 0.7rem 0.9rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  min-width: 0;
}

button {
  font-family: inherit;
  font-size: 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 0.7rem 1.1rem;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

@media (max-width: 420px) {
  #searchInput {
    flex-basis: 100%;
  }
  #searchForm button {
    flex: 1;
  }
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

.current-city {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

h2 {
  font-size: 1.05rem;
  margin: 0 0 0.6rem;
}

.results-list, .availability-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.result-item {
  display: flex;
  gap: 0.8rem;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  width: 100%;
  font: inherit;
  color: inherit;
  margin: 0;
}

.result-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.result-item img {
  width: 40px;
  height: 56px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--border);
  flex-shrink: 0;
}

.result-item .meta {
  min-width: 0;
}

.result-item .title {
  font-weight: 600;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-item .authors, .result-item .isbn {
  font-size: 0.8rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-item.disabled {
  opacity: 0.5;
  cursor: default;
}

.availability-book {
  font-weight: 600;
  margin-bottom: 0.4rem;
}

.availability-status {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.6rem;
}

.lib-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  box-shadow: var(--shadow);
}

.lib-item .lib-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.lib-item .lib-name a {
  color: var(--text);
  text-decoration: none;
}

.lib-item .lib-name a:hover {
  text-decoration: underline;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.8rem;
  white-space: nowrap;
  color: #fff;
  background: var(--text-muted);
}

.badge.ok { background: var(--ok); }
.badge.busy { background: var(--danger); }
.badge.warn { background: var(--warn); color: #1c1c1c; }

.lib-item .system-name {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
}

.reserve-link {
  font-size: 0.8rem;
  margin-top: 0.3rem;
}

.reserve-link a {
  color: var(--accent);
}

.message {
  text-align: center;
  color: var(--text-muted);
  padding: 1.5rem 0;
}

.app-footer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 3rem;
}

.app-footer a { color: var(--text-muted); }

.app-version {
  margin: 0.4rem 0 0;
  font-size: 0.7rem;
  opacity: 0.6;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}

.modal[hidden] {
  display: none;
}

@media (min-width: 480px) {
  .modal { align-items: center; }
}

.modal-content {
  background: var(--surface);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  max-height: 90dvh; /* accounts for the on-screen keyboard on mobile; vh above is the fallback */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 16px 16px 0 0;
  padding: 1.5rem;
  position: relative;
  outline: none;
}

body.modal-open {
  overflow: hidden;
}

@media (min-width: 480px) {
  .modal-content { border-radius: 16px; }
}

.close-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
}

.settings-group {
  margin-bottom: 1.5rem;
}

.settings-group label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.4rem;
  font-size: 0.9rem;
}

.settings-group input[type="text"],
.settings-group input[type="password"],
.settings-group select {
  width: 100%;
  padding: 0.6rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
}

.settings-group button {
  width: 100%;
}

.key-btn-row {
  display: flex;
  gap: 0.5rem;
}

.key-btn-row button {
  width: auto;
  flex: 1;
}

.save-confirm {
  font-size: 0.75rem;
  color: var(--ok);
  margin: 0.3rem 0 0;
}

.hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 0.3rem 0 0.6rem;
}

.hint a { color: var(--accent); }

.systems-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin: 0.4rem 0 0.8rem;
}

.systems-list label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 400;
  font-size: 0.85rem;
  margin: 0;
}

.systems-list input[type="checkbox"] {
  width: auto;
  margin: 0;
}
