$intersection-gap: 28px;
$gutter-margin: $intersection-gap - 3px;
$base-margin: $intersection-gap - 10px;
$board-margin: $base-margin + $gutter-margin;

$board-lighter-color: hsl(41, 36%, 44%);
$board-darker-color: hsl(41, 36%, 40%);

.tenuki-dom-renderer {
  &.tenuki-board {
    position: relative;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    overflow: hidden;
    cursor: default;
  }

  &.tenuki-board .tenuki-inner-container {
    background: rgb(235, 201, 138);
  }

  &.tenuki-board .tenuki-zoom-container {
    transition: transform 0.2s ease-in-out;
    /*
     * address an issue somewhat related to the shrink-to-fit problem on iOS 9: https://forums.developer.apple.com/thread/13510
     * by setting this initial scale, any subsequent shrinking will not leave whitespace on the right of the board, which leads
     * to a horizontal scrollbar
     */
    transform: scale(1);
  }

  &.tenuki-board[data-include-coordinates=true] .cancel-zoom.visible,
  &.tenuki-board[data-include-coordinates=true] .cancel-zoom.visible + .cancel-zoom-backdrop {
    /* additional gutter margin offset */
    margin-left: $gutter-margin;
    margin-bottom: $gutter-margin;
  }

  &.tenuki-board .lines {
    position: absolute;
    top: 0;
    left: 0;
    margin: $base-margin;
  }

  &.tenuki-board[data-include-coordinates=true] .lines {
    /* base margin + additional gutter margin */
    margin: $board-margin;
  }

  &.tenuki-board .line {
    background: $board-lighter-color;
    float: left;
    position: relative;
  }

  &.tenuki-board .line.horizontal {
    height: 1px;
    width: 100%;
    margin-bottom: $intersection-gap;
    clear: left;
  }

  &.tenuki-board .line.vertical {
    width: 1px;
    height: 100%;
    margin-right: $intersection-gap;
  }

  &.tenuki-board .line.vertical:last-child {
    margin-right: -1px;
  }

  &.tenuki-board[data-include-coordinates=true] .line.vertical:before,
  &.tenuki-board[data-include-coordinates=true] .line.vertical:after,
  &.tenuki-board[data-include-coordinates=true] .line.horizontal:before,
  &.tenuki-board[data-include-coordinates=true] .line.horizontal:after {
    width: $gutter-margin - 5px;
    /* gutter margin, less 5px */
    height: $gutter-margin - 5px;
    text-align: center;
    /* gutter margin, less 5px */
    line-height: $gutter-margin - 5px;
    font-family: sans-serif;
    color: $board-darker-color;
    display: block;
    font-size: 14px;
  }

  &.tenuki-board[data-include-coordinates=true] .line.vertical:before,
  &.tenuki-board[data-include-coordinates=true] .line.vertical:after {
    content: attr(data-top-gutter);
  }

  &.tenuki-board[data-include-coordinates=true] .line.horizontal:before,
  &.tenuki-board[data-include-coordinates=true] .line.horizontal:after {
    content: attr(data-left-gutter);
  }

  &.tenuki-board[data-include-coordinates=true] .line.vertical:before {
    /* -1*width/2 */
    margin-left: -1*($gutter-margin - 5px)/2;
    /* -1*width/2, less the intersection gap size */
    margin-top: -1*($gutter-margin - 5px)/2 - $intersection-gap;
  }

  &.tenuki-board[data-include-coordinates=true] .line.vertical:after {
    position: absolute;
    bottom: 0;
    left: 0;
    /* -1*width/2 */
    margin-left: -1*($gutter-margin - 5px)/2;
    /* -1*width/2, less the intersection gap size */
    margin-bottom: -1*($gutter-margin - 5px)/2 - $intersection-gap;
  }

  &.tenuki-board[data-include-coordinates=true] .line.horizontal:before {
    /* -1*width/2, less the intersection gap size */
    margin-left: -1*($gutter-margin - 5px)/2 - $intersection-gap;
    /* -1*width/2 */
    margin-top: -1*($gutter-margin - 5px)/2;
  }

  &.tenuki-board[data-include-coordinates=true] .line.horizontal:after {
    position: absolute;
    top: 0;
    right: 0;
    /* -1*width/2, less the intersection gap size */
    margin-right: -1*($gutter-margin - 5px)/2 - $intersection-gap;
    /* -1*width/2 */
    margin-top: -1*($gutter-margin - 5px)/2;
  }

  &.tenuki-board .hoshi {
    /* -2px to account for the hoshi's own width */
    margin-top: $base-margin - 2px;
    margin-left: $base-margin - 2px;
  }

  &.tenuki-board[data-include-coordinates=true] .hoshi {
    /* -2px to account for the hoshi's own width */
    margin-top: $board-margin - 2px;
    margin-left: $board-margin - 2px;
  }

  &.tenuki-board .hoshi {
    width: calc(2 * 2px + 1px);
    height: calc(2 * 2px + 1px);
    border-radius: 50%;
    background: $board-darker-color;
    position: absolute;
  }

  &.tenuki-board .intersections {
    position: absolute;
    /* base margin, less half the intersection gap size */
    top: $base-margin - $intersection-gap/2;
    /* base margin, less half the intersection gap size */
    left: $base-margin - $intersection-gap/2;
  }

  &.tenuki-board[data-include-coordinates=true] .intersections {
    /* additional gutter margin */
    margin-top: $gutter-margin;
    /* additional gutter margin */
    margin-left: $gutter-margin;
  }

  &.tenuki-board .intersection {
    width: $intersection-gap + 1px;
    height: $intersection-gap + 1px;
  }

  &.tenuki-board-nonflat,
  &.tenuki-board-nonflat .tenuki-inner-container,
  &.tenuki-board-nonflat .tenuki-zoom-container,
  &.tenuki-board-nonflat .intersections {
    /* scale(1) is for the same iOS 9 reason as above */
    transform: scale(1) translate3d(0, 0, 0);
  }

  &.tenuki-board-nonflat.tenuki-smaller-stones .intersection {
    width: $intersection-gap + 1px - 2px;
    height: $intersection-gap + 1px - 2px;
    border: 1px solid transparent;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .occupied {
    transition: 0.1s margin;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.played {
    transition: none;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.v-shift-up {
    margin-top: -1px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.v-shift-upup {
    margin-top: -2px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.v-shift-down {
    margin-top: 1px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.v-shift-downdown {
    margin-top: 2px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.v-shift-none {
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.h-shift-left {
    margin-left: -1px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.h-shift-leftleft {
    margin-left: -2px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.h-shift-right {
    margin-left: 1px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.h-shift-rightright {
    margin-left: 2px;
  }

  &.tenuki-board-nonflat.tenuki-fuzzy-placement .intersection.h-shift-none {
  }

  &.tenuki-board .intersection,
  &.tenuki-board .intersection .stone {
    position: absolute;
  }

  &.tenuki-board .occupied .stone,
  &.tenuki-board .empty.hovered .stone {
    content: "";
    display: block;
    border-radius: 50%;
    border: 1px solid black;
    width: calc(100% - 2px);
    height: calc(100% - 2px);
  }

  &.tenuki-board .intersection.occupied.played .stone:after {
    content: "";
    display: block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    border: 2px solid;
    position: absolute;
    /*
     * 100% of the stone width + 2px for its border is an odd number of pixels,
     * so we go to (n+1)/2 which brings us 1 pixel to the right of, and 1 pixel
     * below the intersection we want.
     */
    left: calc((100% + 2px + 1px)/2);
    top: calc((100% + 2px + 1px)/2);
    /* now correct by the 1px, plus the width of the played marker */
    margin-left: -1px - 5px;
    margin-top: -1px - 5px;
    z-index: 2;
  }

  &.tenuki-board .intersection.white.occupied.played .stone:after {
    color: black;
    background: black;
  }

  &.tenuki-board .intersection.black.occupied.played .stone:after {
    color: white;
    background: white;
  }

  &.tenuki-board .occupied.white .stone,
  &.tenuki-board .empty.hovered.white .stone {
    background: white;
    border-color: #676767;
  }

  &.tenuki-board .empty.hovered.white .stone {
    border-color: black;
  }

  &.tenuki-board .occupied.black .stone,
  &.tenuki-board .empty.hovered.black .stone {
    background: black;
  }

  &.tenuki-board .intersection.empty.hovered .stone,
  &.tenuki-board .intersection.dead .stone {
    opacity: 0.5;
  }

  &.tenuki-board-nonflat .intersection.dead .stone {
    opacity: 0.7;
  }

  &.tenuki-board .intersection.ko .stone {
    background: none !important;
    width: 11px !important;
    height: 11px !important;
    border: 1px solid black !important;
    border-radius: 0 !important;
    left: 50% !important;
    top: 50% !important;
    opacity: 1.0 !important;
    position: absolute;
    margin-top: calc(-1px - 6px);
    margin-left: calc(-1px - 6px);
  }

  &.tenuki-board .intersection.occupied.black.dead .stone,
  &.tenuki-board .intersection.occupied.black.white .stone {
    border-color: rgba(0, 0, 0, 0.5);
  }

  &.tenuki-board .intersection.occupied.black.dead .stone {
    background: rgba(0, 0, 0, 0.5);
  }

  &.tenuki-board .intersection.occupied.white.dead .stone {
    background: rgba(255, 255, 255, 0.5);
  }

  &.tenuki-board .intersection.territory-black:after,
  &.tenuki-board .intersection.territory-white:after {
    content: "";
    display: block;
    width: calc(25% + 4px);
    height: calc(25% + 4px);
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: calc((-25% - 4px)/2);
    margin-top: calc((-25% - 4px)/2);
  }

  &.tenuki-board .intersection.territory-black:after {
    background: black;
  }

  &.tenuki-board .intersection.territory-white:after {
    background: white;
  }

  &.tenuki-board-nonflat .occupied .stone {
    box-shadow: 0px 1.5px 0px rgba(62, 62, 62, 0.38);
  }

  &.tenuki-board-nonflat .intersection.dead .stone {
    box-shadow: 0px 1.5px 0px rgba(62, 62, 62, 0.38 / 2);
  }

  &.tenuki-board-nonflat .occupied.black .stone,
  &.tenuki-board-nonflat .empty.hovered.black .stone {
    border-color: hsl(0, 0%, 28%);
  }

  &.tenuki-board-nonflat .occupied.white .stone,
  &.tenuki-board-nonflat .empty.hovered.white .stone {
    border-color: #DEDEDE;
  }

  &.tenuki-board-nonflat .occupied .stone:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    z-index: 2;
  }

  &.tenuki-board-nonflat .occupied.black .stone:before {
    background: radial-gradient(circle at 50% 15%, hsl(0, 0%, 38%), #39363D 50%);
  }

  &.tenuki-board-nonflat .occupied.white .stone:before {
    background: radial-gradient(circle at 50% 15%, #FFFFFF, #fafdfc 70%);
  }
}
